Merge branch 'master' into sismics_prod

This commit is contained in:
jendib 2016-12-05 19:30:15 +01:00
commit 4f7fcbfdf0
11 changed files with 74 additions and 36 deletions

View File

@ -1,2 +0,0 @@
#!/bin/sh
docker build -t sismics/docs .

View File

@ -64,7 +64,7 @@ public class PdfUtil {
PDFTextStripper stripper = new PDFTextStripper(); PDFTextStripper stripper = new PDFTextStripper();
pdfDocument = PDDocument.load(inputStream); pdfDocument = PDDocument.load(inputStream);
content = stripper.getText(pdfDocument); content = stripper.getText(pdfDocument);
} catch (IOException e) { } catch (Exception e) {
log.error("Error while extracting text from the PDF", e); log.error("Error while extracting text from the PDF", e);
} finally { } finally {
if (pdfDocument != null) { if (pdfDocument != null) {

View File

@ -19,4 +19,6 @@ public class MimeType {
public static final String OPEN_DOCUMENT_TEXT = "application/vnd.oasis.opendocument.text"; public static final String OPEN_DOCUMENT_TEXT = "application/vnd.oasis.opendocument.text";
public static final String OFFICE_DOCUMENT = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"; public static final String OFFICE_DOCUMENT = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
public static final String DEFAULT = "application/octet-stream";
} }

View File

@ -60,7 +60,7 @@ public class MimeTypeUtil {
return MimeType.APPLICATION_PDF; return MimeType.APPLICATION_PDF;
} }
return null; return MimeType.DEFAULT;
} }
/** /**

View File

@ -41,6 +41,7 @@ import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.URISyntaxException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.text.MessageFormat; import java.text.MessageFormat;
@ -129,9 +130,6 @@ public class FileResource extends BaseResource {
} catch (IOException e) { } catch (IOException e) {
throw new ServerException("ErrorGuessMime", "Error guessing mime type", e); throw new ServerException("ErrorGuessMime", "Error guessing mime type", e);
} }
if (mimeType == null) {
throw new ClientException("InvalidFileType", "File type not recognized");
}
// Validate quota // Validate quota
if (user.getStorageCurrent() + fileData.length > user.getStorageQuota()) { if (user.getStorageCurrent() + fileData.length > user.getStorageQuota()) {
@ -535,7 +533,11 @@ public class FileResource extends BaseResource {
mimeType = MimeType.IMAGE_JPEG; // Thumbnails are JPEG mimeType = MimeType.IMAGE_JPEG; // Thumbnails are JPEG
decrypt = true; // Thumbnails are encrypted decrypt = true; // Thumbnails are encrypted
if (!Files.exists(storedFile)) { if (!Files.exists(storedFile)) {
storedFile = Paths.get(getClass().getResource("/image/file.png").getFile()); try {
storedFile = Paths.get(getClass().getResource("/image/file.png").toURI());
} catch (URISyntaxException e) {
// Ignore
}
mimeType = MimeType.IMAGE_PNG; mimeType = MimeType.IMAGE_PNG;
decrypt = false; decrypt = false;
} }

View File

@ -94,6 +94,7 @@ angular.module('docs').controller('DocumentEdit', function($rootScope, $scope, $
// Upload files after edition // Upload files after edition
promise.then(function(data) { promise.then(function(data) {
console.log('document created, adding file', $scope.newFiles);
$scope.fileProgress = 0; $scope.fileProgress = 0;
// When all files upload are over, attach orphan files and move on // When all files upload are over, attach orphan files and move on
@ -133,6 +134,7 @@ angular.module('docs').controller('DocumentEdit', function($rootScope, $scope, $
}; };
// Build the payload // Build the payload
console.log('sending file', key, $scope.newFiles[key], data);
var file = $scope.newFiles[key]; var file = $scope.newFiles[key];
var formData = new FormData(); var formData = new FormData();
formData.append('id', data.id); formData.append('id', data.id);
@ -147,6 +149,7 @@ angular.module('docs').controller('DocumentEdit', function($rootScope, $scope, $
contentType: false, contentType: false,
processData: false, processData: false,
success: function(response) { success: function(response) {
console.log('file uploaded successfully', formData);
deferred.resolve(response); deferred.resolve(response);
}, },
error: function(jqXHR) { error: function(jqXHR) {
@ -192,11 +195,13 @@ angular.module('docs').controller('DocumentEdit', function($rootScope, $scope, $
var then = function() { var then = function() {
key++; key++;
if ($scope.newFiles[key]) { if ($scope.newFiles[key]) {
console.log('sending new file');
sendFile(key).then(then); sendFile(key).then(then);
} else { } else {
$scope.fileIsUploading = false; $scope.fileIsUploading = false;
$scope.fileProgress = 0; $scope.fileProgress = 0;
$rootScope.pageTitle = 'Sismics Docs'; $rootScope.pageTitle = 'Sismics Docs';
console.log('finished sending files, bye');
navigateNext(); navigateNext();
} }
}; };

View File

@ -40,9 +40,7 @@
<div class="form-group"> <div class="form-group">
<label class="col-sm-2 control-label" for="inputFiles">New files</label> <label class="col-sm-2 control-label" for="inputFiles">New files</label>
<div class="col-sm-6"> <div class="col-sm-6">
<file class="form-control" id="inputFiles" multiple="multiple" ng-model="newFiles" <file class="form-control" id="inputFiles" multiple="multiple" ng-model="newFiles" ng-disabled="fileIsUploading"></file>
accept="image/png,image/jpg,image/jpeg,image/gif,application/pdf,application/vnd.oasis.opendocument.text,application/vnd.openxmlformats-officedocument.wordprocessingml.document"
ng-disabled="fileIsUploading"></file>
</div> </div>
<div class="col-sm-4" ng-if="orphanFiles.length > 0"> <div class="col-sm-4" ng-if="orphanFiles.length > 0">
+ {{ orphanFiles.length }} file{{ orphanFiles.length > 1 ? 's' : '' }} + {{ orphanFiles.length }} file{{ orphanFiles.length > 1 ? 's' : '' }}

View File

@ -39,7 +39,7 @@
</dl> </dl>
<div ng-file-drop drag-over-class="bg-success" ng-multiple="true" allow-dir="false" ng-model="dropFiles" <div ng-file-drop drag-over-class="bg-success" ng-multiple="true" allow-dir="false" ng-model="dropFiles"
accept="image/*,application/pdf,application/zip" ng-file-change="fileDropped($files, $event, $rejectedFiles)"> ng-file-change="fileDropped($files, $event, $rejectedFiles)">
<div class="row upload-zone" ui-sortable="fileSortableOptions" ng-model="files"> <div class="row upload-zone" ui-sortable="fileSortableOptions" ng-model="files">
<div class="col-xs-6 col-sm-4 col-md-4 col-lg-3 text-center" ng-repeat="file in files"> <div class="col-xs-6 col-sm-4 col-md-4 col-lg-3 text-center" ng-repeat="file in files">
<div class="thumbnail" ng-if="file.id"> <div class="thumbnail" ng-if="file.id">

View File

@ -36,7 +36,7 @@ public class TestFileResource extends BaseJerseyTest {
/** /**
* Test the file resource. * Test the file resource.
* *
* @throws Exception * @throws Exception e
*/ */
@Test @Test
public void testFileResource() throws Exception { public void testFileResource() throws Exception {
@ -124,7 +124,7 @@ public class TestFileResource extends BaseJerseyTest {
// Check that the files are not readable directly from FS // Check that the files are not readable directly from FS
Path storedFile = DirectoryUtil.getStorageDirectory().resolve(file1Id); Path storedFile = DirectoryUtil.getStorageDirectory().resolve(file1Id);
try (InputStream storedFileInputStream = new BufferedInputStream(Files.newInputStream(storedFile))) { try (InputStream storedFileInputStream = new BufferedInputStream(Files.newInputStream(storedFile))) {
Assert.assertNull(MimeTypeUtil.guessMimeType(storedFileInputStream)); Assert.assertEquals(MimeType.DEFAULT, MimeTypeUtil.guessMimeType(storedFileInputStream));
} }
// Get all files from a document // Get all files from a document
@ -198,16 +198,56 @@ public class TestFileResource extends BaseJerseyTest {
Assert.assertEquals(1, files.size()); Assert.assertEquals(1, files.size());
} }
/**
* Test using a ZIP file.
*
* @throws Exception e
*/
@Test
public void testZipFile() throws Exception {
// Login file1
clientUtil.createUser("file2");
String file2Token = clientUtil.login("file2");
// Create a document
long create1Date = new Date().getTime();
JsonObject json = target().path("/document").request()
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, file2Token)
.put(Entity.form(new Form()
.param("title", "File test document 1")
.param("language", "eng")
.param("create_date", Long.toString(create1Date))), JsonObject.class);
String document1Id = json.getString("id");
Assert.assertNotNull(document1Id);
// Add a file
String file1Id;
try (InputStream is = Resources.getResource("file/wikipedia.zip").openStream()) {
StreamDataBodyPart streamDataBodyPart = new StreamDataBodyPart("file", is, "wikipedia.zip");
try (FormDataMultiPart multiPart = new FormDataMultiPart()) {
json = target()
.register(MultiPartFeature.class)
.path("/file").request()
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, file2Token)
.put(Entity.entity(multiPart.field("id", document1Id).bodyPart(streamDataBodyPart),
MediaType.MULTIPART_FORM_DATA_TYPE), JsonObject.class);
file1Id = json.getString("id");
Assert.assertNotNull(file1Id);
Assert.assertEquals(525069L, json.getJsonNumber("size").longValue());
}
}
}
/** /**
* Test orphan files (without linked document). * Test orphan files (without linked document).
* *
* @throws Exception * @throws Exception e
*/ */
@Test @Test
public void testOrphanFile() throws Exception { public void testOrphanFile() throws Exception {
// Login file2 // Login file3
clientUtil.createUser("file2"); clientUtil.createUser("file3");
String file2Token = clientUtil.login("file2"); String file3Token = clientUtil.login("file3");
// Add a file // Add a file
String file1Id; String file1Id;
@ -217,7 +257,7 @@ public class TestFileResource extends BaseJerseyTest {
JsonObject json = target() JsonObject json = target()
.register(MultiPartFeature.class) .register(MultiPartFeature.class)
.path("/file").request() .path("/file").request()
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, file2Token) .cookie(TokenBasedSecurityFilter.COOKIE_NAME, file3Token)
.put(Entity.entity(multiPart.bodyPart(streamDataBodyPart), .put(Entity.entity(multiPart.bodyPart(streamDataBodyPart),
MediaType.MULTIPART_FORM_DATA_TYPE), JsonObject.class); MediaType.MULTIPART_FORM_DATA_TYPE), JsonObject.class);
file1Id = json.getString("id"); file1Id = json.getString("id");
@ -227,14 +267,14 @@ public class TestFileResource extends BaseJerseyTest {
// Get all orphan files // Get all orphan files
JsonObject json = target().path("/file/list").request() JsonObject json = target().path("/file/list").request()
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, file2Token) .cookie(TokenBasedSecurityFilter.COOKIE_NAME, file3Token)
.get(JsonObject.class); .get(JsonObject.class);
JsonArray files = json.getJsonArray("files"); JsonArray files = json.getJsonArray("files");
Assert.assertEquals(1, files.size()); Assert.assertEquals(1, files.size());
// Get the file data // Get the file data
Response response = target().path("/file/" + file1Id + "/data").request() Response response = target().path("/file/" + file1Id + "/data").request()
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, file2Token) .cookie(TokenBasedSecurityFilter.COOKIE_NAME, file3Token)
.get(); .get();
InputStream is = (InputStream) response.getEntity(); InputStream is = (InputStream) response.getEntity();
byte[] fileBytes = ByteStreams.toByteArray(is); byte[] fileBytes = ByteStreams.toByteArray(is);
@ -243,7 +283,7 @@ public class TestFileResource extends BaseJerseyTest {
// Create a document // Create a document
json = target().path("/document").request() json = target().path("/document").request()
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, file2Token) .cookie(TokenBasedSecurityFilter.COOKIE_NAME, file3Token)
.put(Entity.form(new Form() .put(Entity.form(new Form()
.param("title", "File test document 1") .param("title", "File test document 1")
.param("language", "eng")), JsonObject.class); .param("language", "eng")), JsonObject.class);
@ -252,7 +292,7 @@ public class TestFileResource extends BaseJerseyTest {
// Attach a file to a document // Attach a file to a document
target().path("/file/" + file1Id).request() target().path("/file/" + file1Id).request()
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, file2Token) .cookie(TokenBasedSecurityFilter.COOKIE_NAME, file3Token)
.post(Entity.form(new Form() .post(Entity.form(new Form()
.param("id", document1Id)), JsonObject.class); .param("id", document1Id)), JsonObject.class);
@ -260,7 +300,7 @@ public class TestFileResource extends BaseJerseyTest {
json = target().path("/file/list") json = target().path("/file/list")
.queryParam("id", document1Id) .queryParam("id", document1Id)
.request() .request()
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, file2Token) .cookie(TokenBasedSecurityFilter.COOKIE_NAME, file3Token)
.get(JsonObject.class); .get(JsonObject.class);
files = json.getJsonArray("files"); files = json.getJsonArray("files");
Assert.assertEquals(1, files.size()); Assert.assertEquals(1, files.size());
@ -273,7 +313,7 @@ public class TestFileResource extends BaseJerseyTest {
json = target() json = target()
.register(MultiPartFeature.class) .register(MultiPartFeature.class)
.path("/file").request() .path("/file").request()
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, file2Token) .cookie(TokenBasedSecurityFilter.COOKIE_NAME, file3Token)
.put(Entity.entity(multiPart.bodyPart(streamDataBodyPart), .put(Entity.entity(multiPart.bodyPart(streamDataBodyPart),
MediaType.MULTIPART_FORM_DATA_TYPE), JsonObject.class); MediaType.MULTIPART_FORM_DATA_TYPE), JsonObject.class);
file2Id = json.getString("id"); file2Id = json.getString("id");
@ -283,7 +323,7 @@ public class TestFileResource extends BaseJerseyTest {
// Deletes a file // Deletes a file
json = target().path("/file/" + file2Id).request() json = target().path("/file/" + file2Id).request()
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, file2Token) .cookie(TokenBasedSecurityFilter.COOKIE_NAME, file3Token)
.delete(JsonObject.class); .delete(JsonObject.class);
Assert.assertEquals("ok", json.getString("status")); Assert.assertEquals("ok", json.getString("status"));
} }
@ -291,7 +331,7 @@ public class TestFileResource extends BaseJerseyTest {
/** /**
* Test user quota. * Test user quota.
* *
* @throws Exception * @throws Exception e
*/ */
@Test @Test
public void testQuota() throws Exception { public void testQuota() throws Exception {

Binary file not shown.

View File

@ -1,7 +0,0 @@
#!/bin/bash
docker rm -f sismics_docs
docker run \
-d --name=sismics_docs --restart=always \
-v sismics_docs_data:/data \
-e 'VIRTUAL_HOST_SECURE=docs.bgamard.org' -e 'VIRTUAL_PORT=80' \
sismics/docs:latest