Search in sources :

Example 16 with VFile

use of io.vertigo.dynamo.file.model.VFile in project vertigo by KleeGroup.

the class FsFileStorePlugin method read.

/**
 * {@inheritDoc}
 */
@Override
public FileInfo read(final FileInfoURI uri) {
    // récupération de l'objet en base
    final URI<Entity> dtoUri = createDtObjectURI(uri);
    final DtObject fileInfoDto = getStoreManager().getDataStore().readOne(dtoUri);
    // récupération du fichier
    final String fileName = getValue(fileInfoDto, DtoFields.FILE_NAME, String.class);
    final String mimeType = getValue(fileInfoDto, DtoFields.MIME_TYPE, String.class);
    final Instant lastModified = getValue(fileInfoDto, DtoFields.LAST_MODIFIED, Instant.class);
    final Long length = getValue(fileInfoDto, DtoFields.LENGTH, Long.class);
    final String filePath = getValue(fileInfoDto, DtoFields.FILE_PATH, String.class);
    final InputStreamBuilder inputStreamBuilder = new FileInputStreamBuilder(new File(documentRoot + filePath));
    final VFile vFile = fileManager.createFile(fileName, mimeType, lastModified, length, inputStreamBuilder);
    // retourne le fileinfo avec le fichier et son URI
    final FsFileInfo fsFileInfo = new FsFileInfo(uri.getDefinition(), vFile);
    fsFileInfo.setURIStored(uri);
    return fsFileInfo;
}
Also used : Entity(io.vertigo.dynamo.domain.model.Entity) InputStreamBuilder(io.vertigo.dynamo.file.model.InputStreamBuilder) DtObject(io.vertigo.dynamo.domain.model.DtObject) Instant(java.time.Instant) VFile(io.vertigo.dynamo.file.model.VFile) File(java.io.File) VFile(io.vertigo.dynamo.file.model.VFile)

Example 17 with VFile

use of io.vertigo.dynamo.file.model.VFile in project vertigo by KleeGroup.

the class FsFileStorePlugin method createFileInfoEntity.

private Entity createFileInfoEntity(final FileInfo fileInfo) {
    final Entity fileInfoDto = createFileInfoEntity(fileInfo.getDefinition());
    // -----
    final VFile vFile = fileInfo.getVFile();
    setValue(fileInfoDto, DtoFields.FILE_NAME, vFile.getFileName());
    setValue(fileInfoDto, DtoFields.MIME_TYPE, vFile.getMimeType());
    setValue(fileInfoDto, DtoFields.LAST_MODIFIED, vFile.getLastModified());
    setValue(fileInfoDto, DtoFields.LENGTH, vFile.getLength());
    if (fileInfo.getURI() == null) {
        // cas de la création, on ajoute en base un chemin fictif (colonne not null)
        setValue(fileInfoDto, DtoFields.FILE_PATH, "/dev/null");
    } else {
        // cas de l'update
        setIdValue(fileInfoDto, fileInfo.getURI().getKey());
        // récupération de l'objet en base pour récupérer le path du fichier et ne pas modifier la base
        final URI<Entity> dtoUri = createDtObjectURI(fileInfo.getURI());
        final DtObject fileInfoDtoBase = getStoreManager().getDataStore().readOne(dtoUri);
        final String pathToSave = getValue(fileInfoDtoBase, DtoFields.FILE_PATH, String.class);
        setValue(fileInfoDto, DtoFields.FILE_PATH, pathToSave);
    }
    return fileInfoDto;
}
Also used : Entity(io.vertigo.dynamo.domain.model.Entity) DtObject(io.vertigo.dynamo.domain.model.DtObject) VFile(io.vertigo.dynamo.file.model.VFile)

Example 18 with VFile

use of io.vertigo.dynamo.file.model.VFile in project vertigo by KleeGroup.

the class FsFullFileStorePlugin method create.

/**
 * {@inheritDoc}
 */
@Override
public FileInfo create(final FileInfo fileInfo) {
    Assertion.checkNotNull(fileInfo);
    Assertion.checkArgument(fileInfo.getURI() == null, "Only file without any id can be created.");
    // -----
    final VFile vFile = fileInfo.getVFile();
    final DateTimeFormatter formatter = DateTimeFormatter.ofPattern(INFOS_DATE_PATTERN).withZone(ZoneId.of("UTC"));
    final String metaData = new StringBuilder().append(vFile.getFileName()).append('\n').append(vFile.getMimeType()).append('\n').append(formatter.format(vFile.getLastModified())).append('\n').append(vFile.getLength()).append('\n').toString();
    final FileInfoURI uri = createNewFileInfoURI(fileInfo.getDefinition());
    fileInfo.setURIStored(uri);
    saveFile(metaData, fileInfo);
    return fileInfo;
}
Also used : FileInfoURI(io.vertigo.dynamo.domain.model.FileInfoURI) DateTimeFormatter(java.time.format.DateTimeFormatter) VFile(io.vertigo.dynamo.file.model.VFile)

Example 19 with VFile

use of io.vertigo.dynamo.file.model.VFile in project vertigo by KleeGroup.

the class TextAccountStorePlugin method createVFile.

private static Optional<VFile> createVFile(final URI<Account> accountURI, final URL fileURL, final String photoUrl) {
    File photoFile;
    try {
        photoFile = new File(fileURL.toURI());
    } catch (final URISyntaxException e) {
        return Optional.empty();
    }
    Assertion.checkArgument(photoFile.exists(), "Account {0} photo {1} not found", accountURI, photoUrl);
    Assertion.checkArgument(photoFile.isFile(), "Account {0} photo {1} must be a file", accountURI, photoUrl);
    try {
        final String contentType = Files.probeContentType(photoFile.toPath());
        return Optional.of(new FSFile(photoFile.getName(), contentType, photoFile));
    } catch (final IOException e) {
        throw WrappedException.wrap(e);
    }
}
Also used : URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) VFile(io.vertigo.dynamo.file.model.VFile) File(java.io.File) FSFile(io.vertigo.dynamo.impl.file.model.FSFile) FSFile(io.vertigo.dynamo.impl.file.model.FSFile)

Example 20 with VFile

use of io.vertigo.dynamo.file.model.VFile in project vertigo by KleeGroup.

the class FileManagerTest method testCreateTempFileWithNoFile.

@Test
public void testCreateTempFileWithNoFile() {
    final String fileName = "monTestFile.txt";
    final String typeMime = "monTypeMime";
    final Instant lastModified = Instant.now();
    final long length = 123;
    final InputStreamBuilder inputStreamBuilder = new InputStreamBuilder() {

        @Override
        public InputStream createInputStream() {
            return new StringBufferInputStream("Contenu test");
        }
    };
    final VFile vFile = fileManager.createFile(fileName, typeMime, lastModified, length, inputStreamBuilder);
    checkVFile(vFile, fileName, lastModified, typeMime, length);
}
Also used : StringBufferInputStream(java.io.StringBufferInputStream) InputStreamBuilder(io.vertigo.dynamo.file.model.InputStreamBuilder) Instant(java.time.Instant) VFile(io.vertigo.dynamo.file.model.VFile) Test(org.junit.Test)

Aggregations

VFile (io.vertigo.dynamo.file.model.VFile)25 File (java.io.File)12 Entity (io.vertigo.dynamo.domain.model.Entity)7 Test (org.junit.Test)7 InputStreamBuilder (io.vertigo.dynamo.file.model.InputStreamBuilder)6 Instant (java.time.Instant)6 GET (io.vertigo.vega.webservice.stereotype.GET)4 IOException (java.io.IOException)4 URL (java.net.URL)4 DtObject (io.vertigo.dynamo.domain.model.DtObject)3 FileInfoURI (io.vertigo.dynamo.domain.model.FileInfoURI)3 FileInfo (io.vertigo.dynamo.file.model.FileInfo)3 VTransactionWritable (io.vertigo.commons.transaction.VTransactionWritable)2 URI (io.vertigo.dynamo.domain.model.URI)2 FSFile (io.vertigo.dynamo.impl.file.model.FSFile)2 DataStream (io.vertigo.lang.DataStream)2 OutputStream (java.io.OutputStream)2 StringBufferInputStream (java.io.StringBufferInputStream)2 DateTimeFormatter (java.time.format.DateTimeFormatter)2 FileInfoStd (io.vertigo.dynamo.store.data.fileinfo.FileInfoStd)1