All Downloads are FREE. Search and download functionalities are using the official Maven repository.

me.desair.tus.server.upload.disk.AbstractDiskBasedService Maven / Gradle / Ivy

Go to download

Server-side implementation of the open file upload protocol tus (https://tus.io/) that supports resumable file uploads for small and very large files

There is a newer version: 1.0.0-3.0
Show newest version
package me.desair.tus.server.upload.disk;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.UUID;

import me.desair.tus.server.TusFileUploadService;
import org.apache.commons.lang3.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Common abstract super class to implement service that use the disk file system
 */
public class AbstractDiskBasedService {

    private static final Logger log = LoggerFactory.getLogger(TusFileUploadService.class);

    private Path storagePath;

    public AbstractDiskBasedService(String path) {
        Validate.notBlank(path, "The storage path cannot be blank");
        this.storagePath = Paths.get(path);
    }

    protected Path getStoragePath() {
        return storagePath;
    }

    protected Path getPathInStorageDirectory(UUID id) {
        if (!Files.exists(storagePath)) {
            init();
        }

        if (id == null) {
            return null;
        } else {
            return storagePath.resolve(id.toString());
        }
    }

    private synchronized void init() {
        if (!Files.exists(storagePath)) {
            try {
                Files.createDirectories(storagePath);
            } catch (IOException e) {
                String message = "Unable to create the directory specified by the storage path " + storagePath;
                log.error(message, e);
                throw new StoragePathNotAvailableException(message, e);
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy