Search in sources :

Example 1 with StorageException

use of com.bluenimble.platform.storage.StorageException in project serverless by bluenimble.

the class FileSystemFolder method add.

@Override
public StorageObject add(ApiStreamSource ss, String altName, boolean overwrite) throws StorageException {
    if (Lang.isNullOrEmpty(altName) && ss == null) {
        throw new StorageException("object name is required");
    }
    String name = altName != null ? altName : ss.name();
    validateName(name);
    File file = new File(source, name);
    if (file.exists() && !overwrite) {
        throw new StorageException("object '" + name + "' already exists under " + name());
    }
    if (ss == null && !file.exists()) {
        try {
            file.createNewFile();
        } catch (IOException ioex) {
            throw new StorageException(ioex.getMessage(), ioex);
        }
        return new FileSystemStorageObject(file, false);
    }
    OutputStream os = null;
    try {
        os = new FileOutputStream(file);
        IOUtils.copy(ss.stream(), os);
    } catch (IOException ioex) {
        throw new StorageException(ioex.getMessage(), ioex);
    } finally {
        IOUtils.closeQuietly(os);
    }
    return new FileSystemStorageObject(file, false);
}
Also used : OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) StorageException(com.bluenimble.platform.storage.StorageException) File(java.io.File)

Example 2 with StorageException

use of com.bluenimble.platform.storage.StorageException in project serverless by bluenimble.

the class FileSystemFolder method get.

@Override
public StorageObject get(String path) throws StorageException {
    validatePath(path);
    File file = new File(source, path);
    if (!file.exists()) {
        throw new StorageException("object '" + path + "' not found under " + name());
    }
    if (file.isDirectory()) {
        return new FileSystemFolder(file, false);
    }
    return new FileSystemStorageObject(file, false);
}
Also used : File(java.io.File) StorageException(com.bluenimble.platform.storage.StorageException)

Example 3 with StorageException

use of com.bluenimble.platform.storage.StorageException in project serverless by bluenimble.

the class FileSystemFolder method add.

@Override
public Folder add(String path, boolean ignoreIfExists) throws StorageException {
    validatePath(path);
    File folder = new File(source, path);
    if (folder.exists() && !ignoreIfExists) {
        throw new StorageException("folder '" + path + "' already exists under " + name());
    }
    folder.mkdirs();
    if (!folder.exists()) {
        throw new StorageException("unbale to create folder '" + path + "' under " + name());
    }
    return new FileSystemFolder(folder, false);
}
Also used : File(java.io.File) StorageException(com.bluenimble.platform.storage.StorageException)

Example 4 with StorageException

use of com.bluenimble.platform.storage.StorageException in project serverless by bluenimble.

the class FileSystemStorageObject method writer.

@Override
public OutputStream writer(ApiContext context) throws StorageException {
    if (isFolder()) {
        throw new StorageException(name() + " is a folder");
    }
    OutputStream os = null;
    try {
        os = new FileOutputStream(source);
    } catch (IOException ioex) {
        throw new StorageException(ioex.getMessage(), ioex);
    }
    RecyclableOutputStream ros = new RecyclableOutputStream(os);
    context.addRecyclable(Lang.UUID(10), ros);
    return ros;
}
Also used : OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) StorageException(com.bluenimble.platform.storage.StorageException)

Example 5 with StorageException

use of com.bluenimble.platform.storage.StorageException in project serverless by bluenimble.

the class FileSystemStorageObject method reader.

@Override
public InputStream reader(ApiContext context) throws StorageException {
    if (isFolder()) {
        throw new StorageException(name() + " is a folder");
    }
    InputStream is = null;
    try {
        is = new FileInputStream(source);
    } catch (IOException ioex) {
        throw new StorageException(ioex.getMessage(), ioex);
    }
    RecyclableInputStream ris = new RecyclableInputStream(is);
    context.addRecyclable(Lang.UUID(10), ris);
    return ris;
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) StorageException(com.bluenimble.platform.storage.StorageException) FileInputStream(java.io.FileInputStream)

Aggregations

StorageException (com.bluenimble.platform.storage.StorageException)21 StorageObject (com.bluenimble.platform.storage.StorageObject)10 IOException (java.io.IOException)9 ApiServiceExecutionException (com.bluenimble.platform.api.ApiServiceExecutionException)8 Storage (com.bluenimble.platform.storage.Storage)8 JsonObject (com.bluenimble.platform.json.JsonObject)7 ApiAccessDeniedException (com.bluenimble.platform.api.ApiAccessDeniedException)6 ApiSpace (com.bluenimble.platform.api.ApiSpace)6 JsonApiOutput (com.bluenimble.platform.api.impls.JsonApiOutput)6 File (java.io.File)6 Folder (com.bluenimble.platform.storage.Folder)5 ApiStreamSource (com.bluenimble.platform.api.ApiStreamSource)4 FileInputStream (java.io.FileInputStream)4 FileOutputStream (java.io.FileOutputStream)4 FileChannel (java.nio.channels.FileChannel)4 InputStream (java.io.InputStream)3 OutputStream (java.io.OutputStream)3 ApiOutput (com.bluenimble.platform.api.ApiOutput)2 Chunk (com.bluenimble.platform.streams.Chunk)2 ByteBuffer (java.nio.ByteBuffer)2