Commit 551e9e55 authored by Matija Obreza's avatar Matija Obreza
Browse files

Simple file upload and download

parent 3edae7ce
......@@ -26,6 +26,7 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Path;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.commons.io.IOUtils;
import org.genesys.filerepository.InvalidRepositoryFileDataException;
......@@ -71,13 +72,21 @@ public class TemporaryBytesManager {
return new BufferedOutputStream(new FileOutputStream(tempFile)) {
private AtomicBoolean stored=new AtomicBoolean(false);
@Override
public void close() throws IOException {
try {
super.close();
if (stored.getAndSet(true)) {
LOG.debug("Bytes of file={} already synchronized", tempFile.getAbsolutePath());
return;
}
String repositoryPath = parent;
String originalFilename = filename;
ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
LOG.debug("Uploaded file size={}", new File(tempFile.getAbsolutePath()).length());
try (InputStream source = new FileInputStream(tempFile)) {
IOUtils.copy(source, baos);
}
......
......@@ -18,6 +18,7 @@ package org.genesys.filerepository.service.ftp;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
......@@ -31,7 +32,6 @@ import org.apache.commons.net.ftp.FTPReply;
import org.apache.commons.net.ftp.FTPSClient;
import org.genesys.filerepository.config.DatabaseConfig;
import org.genesys.filerepository.config.ServiceBeanConfig;
import org.hsqldb.lib.StringInputStream;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
......@@ -202,9 +202,9 @@ public class FtpServerTest {
assertThat(ftp.printWorkingDirectory(), is("/"));
ftp.setFileTransferMode(FTPClient.BINARY_FILE_TYPE);
ftp.setFileTransferMode(FTPClient.ASCII_FILE_TYPE);
try (InputStream local = new StringInputStream(TEST_FILE_CONTENTS)) {
try (InputStream local = new ByteArrayInputStream(TEST_FILE_CONTENTS.getBytes())) {
assertThat(ftp.storeFile("file", local), is(true));
}
......@@ -217,9 +217,11 @@ public class FtpServerTest {
ByteArrayOutputStream local = new ByteArrayOutputStream(1024);
LOG.info("Retrieving file={}", theFile.getName());
ftp.retrieveFile(theFile.getName(), local);
LOG.debug("size={}", local.size());
LOG.trace("size={}", local.size());
String contents = new String(local.toByteArray());
LOG.debug("toStr={} test={}", contents, TEST_FILE_CONTENTS);
LOG.trace("toStr={} test={}", contents, TEST_FILE_CONTENTS);
LOG.trace("toStr={} test={}", contents.length(), TEST_FILE_CONTENTS.length());
LOG.trace("toStr={} test={}", contents.getBytes(), TEST_FILE_CONTENTS.getBytes());
assertThat(contents, is(TEST_FILE_CONTENTS));
debugListFiles(ftp);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment