file-uploader

What is it

Service used to upload files using HTTP REST multipart requests. It’s main purpose is to act as a simple solution to backup storage. It supports automatic file deletion after customizable file age. Made to work with JWT security using JWK.

Backup storage

Service can act as a (backup) storage for any kind of data. Files are saved to local disk storage - base path is configurable. (See property files.path)

Configurable file age

Data is managed by age - files older than n days are periodically deleted. (See property files.max-days-age)

Configurable file deletion schedule

Scheduled time to run deletion process can be configured using cron expressions. (See property files.deletion-cron-expression) For example, to run deletion process every day at 2 AM:

files.deletion-cron-expression=0 0 2 1/1 * ?

Above cron expression is also set as default (if no property is defined).

Tracing

All upload requests are traced to log files. Data is logged in JSON format. (See property trace.files.path).

Example of trace info - saving file My-file.pdf to directory mydir:

2018-03-13_20:42:25.721 - {"file":"My-file.pdf","directory":"mydir"}

Rest API

Files can be uploaded using HTTP multipart requests. More specifically using API endpoint:

Parameters:

Swagger API documentation

API documents via swagger UI can be accessed via /swagger-ui.html

For instance: http://localhost:8888/swagger-ui.html if you run the service locally on port 8888 that is set as default.

Security

OAuth2 security is supported using JWT (Json Web Tokens). It can be enabled/disabled via spring profiles:

Setup

Properties configuration

All known Spring properties must/can be applied here (server.port, spring.application.name, …) In addition to that, service provides some additional configuration options:

Calling REST API ( via RestTemplate )

If by any chance you are using RestTemplate to call REST API and do not have an actual file but it’s rather already loaded as byte array ( byte[] ) …

Following method will make multipart http request.

public void uploadFile(final byte[] data, final String uri, final String dir) {

    RestTemplate restTemplate = restTemplatesFactory.get();

    ByteArrayResource contentsAsResource = new ByteArrayResource(pdfDto.getData()) {

      @Override
      public String getFilename() { return "my_file_name"; }

    };

    MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
    map.add("name", "my_file_name"));
    map.add("filename", "my_file_name");
    map.add("file", data);

    if (dir != null)
    {
      map.add("directory", dir);
    }

    restTemplate.postForObject(uri, map, ResponseEntity.class);
  }

Tech used