mirror of
https://github.com/sismics/docs.git
synced 2024-11-22 05:57:57 +01:00
PDF handling, file upload progression
This commit is contained in:
parent
19000d095f
commit
471933ca8c
@ -113,8 +113,8 @@
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.bitlet</groupId>
|
||||
<artifactId>weupnp</artifactId>
|
||||
<groupId>org.imgscalr</groupId>
|
||||
<artifactId>imgscalr-lib</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Test dependencies -->
|
||||
|
@ -1,11 +0,0 @@
|
||||
package com.sismics.docs.core.dao.lucene;
|
||||
|
||||
|
||||
/**
|
||||
* Lucene Article DAO.
|
||||
*
|
||||
* @author bgamard
|
||||
*/
|
||||
public class ArticleDao {
|
||||
|
||||
}
|
@ -16,4 +16,6 @@ public class MimeType {
|
||||
public static final String IMAGE_GIF = "image/gif";
|
||||
|
||||
public static final String APPLICATION_ZIP = "application/zip";
|
||||
|
||||
public static final String APPLICATION_PDF = "application/pdf";
|
||||
}
|
||||
|
@ -37,6 +37,8 @@ public class MimeTypeUtil {
|
||||
return MimeType.IMAGE_PNG;
|
||||
} else if (headerBytes[0] == ((byte) 0x00) && headerBytes[1] == ((byte) 0x00) && headerBytes[2] == ((byte) 0x01) && headerBytes[3] == ((byte) 0x00)) {
|
||||
return MimeType.IMAGE_X_ICON;
|
||||
} else if (headerBytes[0] == ((byte) 0x25) && headerBytes[1] == ((byte) 0x50) && headerBytes[2] == ((byte) 0x44) && headerBytes[3] == ((byte) 0x46)) {
|
||||
return MimeType.APPLICATION_PDF;
|
||||
}
|
||||
|
||||
return null;
|
||||
|
@ -34,7 +34,7 @@
|
||||
<com.googlecode.owasp-java-html-sanitizer.owasp-java-html-sanitizer.version>r156</com.googlecode.owasp-java-html-sanitizer.owasp-java-html-sanitizer.version>
|
||||
<org.apache.lucene.version>4.2.0</org.apache.lucene.version>
|
||||
<jgoodies.forms.version>1.0.5</jgoodies.forms.version>
|
||||
<org.bitlet.weupnp.version>0.1.2</org.bitlet.weupnp.version>
|
||||
<org.imgscalr.imgscalr-lib.version>4.2</org.imgscalr.imgscalr-lib.version>
|
||||
|
||||
<com.sun.grizzly.version>1.9.18-m</com.sun.grizzly.version>
|
||||
<org.hibernate.hibernate.version>4.1.0.Final</org.hibernate.hibernate.version>
|
||||
@ -430,9 +430,9 @@
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.bitlet</groupId>
|
||||
<artifactId>weupnp</artifactId>
|
||||
<version>${org.bitlet.weupnp.version}</version>
|
||||
<groupId>org.imgscalr</groupId>
|
||||
<artifactId>imgscalr-lib</artifactId>
|
||||
<version>${org.imgscalr.imgscalr-lib.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
@ -105,21 +105,24 @@ public class RequestContextFilter implements Filter {
|
||||
} catch (Exception e) {
|
||||
ThreadLocalContext.cleanup();
|
||||
|
||||
log.error("An exception occured, rolling back current transaction", e);
|
||||
// IOException are thrown if the client closes the connection before completion
|
||||
if (!(e instanceof IOException)) {
|
||||
log.error("An exception occured, rolling back current transaction", e);
|
||||
|
||||
// If an unprocessed error comes up from the application layers (Jersey...), rollback the transaction (should not happen)
|
||||
if (em.isOpen()) {
|
||||
if (em.getTransaction() != null && em.getTransaction().isActive()) {
|
||||
em.getTransaction().rollback();
|
||||
}
|
||||
// If an unprocessed error comes up from the application layers (Jersey...), rollback the transaction (should not happen)
|
||||
if (em.isOpen()) {
|
||||
if (em.getTransaction() != null && em.getTransaction().isActive()) {
|
||||
em.getTransaction().rollback();
|
||||
}
|
||||
|
||||
try {
|
||||
em.close();
|
||||
} catch (Exception ce) {
|
||||
log.error("Error closing entity manager", ce);
|
||||
try {
|
||||
em.close();
|
||||
} catch (Exception ce) {
|
||||
log.error("Error closing entity manager", ce);
|
||||
}
|
||||
}
|
||||
throw new ServletException(e);
|
||||
}
|
||||
throw new ServletException(e);
|
||||
}
|
||||
|
||||
ThreadLocalContext.cleanup();
|
||||
|
@ -1,9 +1,7 @@
|
||||
package com.sismics.docs.rest.resource;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.FilenameFilter;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.text.MessageFormat;
|
||||
import java.util.ArrayList;
|
||||
@ -33,6 +31,8 @@ import com.sismics.rest.exception.ClientException;
|
||||
import com.sismics.rest.exception.ForbiddenClientException;
|
||||
import com.sismics.rest.exception.ServerException;
|
||||
import com.sismics.rest.util.ValidationUtil;
|
||||
import com.sismics.util.FileUtil;
|
||||
import com.sismics.util.ImageUtil;
|
||||
import com.sismics.util.mime.MimeTypeUtil;
|
||||
import com.sun.jersey.multipart.FormDataBodyPart;
|
||||
import com.sun.jersey.multipart.FormDataParam;
|
||||
@ -129,8 +129,8 @@ public class FileResource extends BaseResource {
|
||||
file.setMimeType(mimeType);
|
||||
String fileId = fileDao.create(file);
|
||||
|
||||
// Copy the incoming stream content into the storage directory
|
||||
Files.copy(is, Paths.get(DirectoryUtil.getStorageDirectory().getPath(), fileId));
|
||||
// Save the file
|
||||
FileUtil.save(is, file);
|
||||
|
||||
// Always return ok
|
||||
JSONObject response = new JSONObject();
|
||||
@ -222,22 +222,36 @@ public class FileResource extends BaseResource {
|
||||
@Path("{id: [a-z0-9\\-]+}/data")
|
||||
@Produces(MediaType.APPLICATION_OCTET_STREAM)
|
||||
public Response data(
|
||||
@PathParam("id") final String id) throws JSONException {
|
||||
@PathParam("id") final String id,
|
||||
@QueryParam("thumbnail") boolean thumbnail) throws JSONException {
|
||||
if (!authenticate()) {
|
||||
throw new ForbiddenClientException();
|
||||
}
|
||||
|
||||
// Get the file
|
||||
java.io.File storageDirectory = DirectoryUtil.getStorageDirectory();
|
||||
java.io.File[] matchingFiles = storageDirectory.listFiles(new FilenameFilter() {
|
||||
@Override
|
||||
public boolean accept(java.io.File dir, String name) {
|
||||
return name.startsWith(id);
|
||||
}
|
||||
});
|
||||
final java.io.File storageFile = matchingFiles[0];
|
||||
FileDao fileDao = new FileDao();
|
||||
File file = null;
|
||||
try {
|
||||
file = fileDao.getFile(id);
|
||||
} catch (NoResultException e) {
|
||||
throw new ClientException("FileNotFound", MessageFormat.format("File not found: {0}", id));
|
||||
}
|
||||
|
||||
return Response.ok(storageFile)
|
||||
|
||||
// Get the stored file
|
||||
java.io.File storedfile = null;
|
||||
if (thumbnail) {
|
||||
if (ImageUtil.isImage(file.getMimeType())) {
|
||||
storedfile = Paths.get(DirectoryUtil.getStorageDirectory().getPath(), id + "_thumb").toFile();
|
||||
} else {
|
||||
storedfile = new java.io.File(getClass().getResource("/image/file.png").getFile());
|
||||
}
|
||||
} else {
|
||||
storedfile = Paths.get(DirectoryUtil.getStorageDirectory().getPath(), id).toFile();
|
||||
}
|
||||
|
||||
return Response.ok(storedfile)
|
||||
.header("Content-Type", file.getMimeType())
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
@ -67,14 +67,10 @@ var App = angular.module('docs', ['ui.state', 'ui.bootstrap', 'ui.keypress', 're
|
||||
})
|
||||
.state('document.view.file', {
|
||||
url: '/file/:fileId',
|
||||
onEnter: function($stateParams, $state, $dialog) {
|
||||
$dialog.dialog({
|
||||
keyboard: true,
|
||||
templateUrl: 'partial/file.view.html',
|
||||
views: {
|
||||
'file': {
|
||||
controller: 'FileView'
|
||||
}).open().then(function(result) {
|
||||
$state.transitionTo('document.view', { id: $stateParams.id });
|
||||
});
|
||||
}
|
||||
}
|
||||
})
|
||||
.state('login', {
|
||||
|
@ -3,7 +3,7 @@
|
||||
/**
|
||||
* Document edition controller.
|
||||
*/
|
||||
App.controller('DocumentEdit', function($scope, $http, $state, $stateParams, Restangular) {
|
||||
App.controller('DocumentEdit', function($scope, $q, $http, $state, $stateParams, Restangular) {
|
||||
/**
|
||||
* Returns true if in edit mode (false in add mode).
|
||||
*/
|
||||
@ -28,37 +28,55 @@ App.controller('DocumentEdit', function($scope, $http, $state, $stateParams, Res
|
||||
|
||||
if ($scope.isEdit()) {
|
||||
promise = Restangular
|
||||
.one('document', $stateParams.id)
|
||||
.post('', $scope.document);
|
||||
promise.then(function(data) {
|
||||
$scope.loadDocuments();
|
||||
$state.transitionTo('document.view', { id: $stateParams.id });
|
||||
})
|
||||
.one('document', $stateParams.id)
|
||||
.post('', $scope.document);
|
||||
} else {
|
||||
promise = Restangular
|
||||
.one('document')
|
||||
.put($scope.document);
|
||||
promise.then(function(data) {
|
||||
$scope.document = {};
|
||||
$scope.loadDocuments();
|
||||
});
|
||||
.one('document')
|
||||
.put($scope.document);
|
||||
}
|
||||
|
||||
// Upload files after edition
|
||||
// TODO Handle file upload progression and errors
|
||||
promise.then(function(data) {
|
||||
_.each($scope.files, function(file) {
|
||||
var promises = [];
|
||||
$scope.fileProgress = 0;
|
||||
|
||||
_.each($scope.newFiles, function(file) {
|
||||
// Build the payload
|
||||
var formData = new FormData();
|
||||
formData.append('id', data.id);
|
||||
formData.append('file', file);
|
||||
$.ajax({
|
||||
url: 'api/file',
|
||||
type: 'PUT',
|
||||
data: formData,
|
||||
processData: false,
|
||||
contentType: false
|
||||
|
||||
// Send the file
|
||||
var promiseFile = $http.put('api/file',
|
||||
formData, {
|
||||
headers: { 'Content-Type': false },
|
||||
transformRequest: function(data) { return data; }
|
||||
});
|
||||
|
||||
// TODO Handle progression when $q.notify will be released
|
||||
|
||||
promiseFile.then(function() {
|
||||
$scope.fileProgress += 100 / _.size($scope.newFiles);
|
||||
});
|
||||
|
||||
// Store the promise for later
|
||||
promises.push(promiseFile);
|
||||
});
|
||||
|
||||
// When all files upload are over, move on
|
||||
var promiseAll = $q.all(promises);
|
||||
if ($scope.isEdit()) {
|
||||
promiseAll.then(function(data) {
|
||||
$scope.loadDocuments();
|
||||
$state.transitionTo('document.view', { id: $stateParams.id });
|
||||
});
|
||||
} else {
|
||||
promiseAll.then(function(data) {
|
||||
$scope.document = {};
|
||||
$scope.loadDocuments();
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
|
@ -3,34 +3,62 @@
|
||||
/**
|
||||
* File view controller.
|
||||
*/
|
||||
App.controller('FileView', function($rootScope, $state, $scope, $stateParams) {
|
||||
$scope.id = $stateParams.fileId;
|
||||
App.controller('FileView', function($dialog, $state, $stateParams) {
|
||||
var dialog = $dialog.dialog({
|
||||
keyboard: true,
|
||||
templateUrl: 'partial/file.view.html',
|
||||
controller: function($rootScope, $scope, $state, $stateParams) {
|
||||
$scope.id = $stateParams.fileId;
|
||||
|
||||
/**
|
||||
* Navigate to the next file.
|
||||
*/
|
||||
$scope.nextFile = function() {
|
||||
_.each($rootScope.files, function(value, key, list) {
|
||||
if (value.id == $scope.id) {
|
||||
var next = $rootScope.files[key + 1];
|
||||
if (next) {
|
||||
$state.transitionTo('document.view.file', { id: $stateParams.id, fileId: next.id });
|
||||
// Search current file
|
||||
_.each($rootScope.files, function(value, key, list) {
|
||||
if (value.id == $scope.id) {
|
||||
$scope.file = value;
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
});
|
||||
|
||||
/**
|
||||
* Navigate to the previous file.
|
||||
*/
|
||||
$scope.previousFile = function() {
|
||||
_.each($rootScope.files, function(value, key, list) {
|
||||
if (value.id == $scope.id) {
|
||||
var previous = $rootScope.files[key - 1];
|
||||
if (previous) {
|
||||
$state.transitionTo('document.view.file', { id: $stateParams.id, fileId: previous.id });
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
/**
|
||||
* Navigate to the next file.
|
||||
*/
|
||||
$scope.nextFile = function() {
|
||||
_.each($rootScope.files, function(value, key, list) {
|
||||
if (value.id == $scope.id) {
|
||||
var next = $rootScope.files[key + 1];
|
||||
if (next) {
|
||||
dialog.close({});
|
||||
$state.transitionTo('document.view.file', { id: $stateParams.id, fileId: next.id });
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Navigate to the previous file.
|
||||
*/
|
||||
$scope.previousFile = function() {
|
||||
_.each($rootScope.files, function(value, key, list) {
|
||||
if (value.id == $scope.id) {
|
||||
var previous = $rootScope.files[key - 1];
|
||||
if (previous) {
|
||||
dialog.close({});
|
||||
$state.transitionTo('document.view.file', { id: $stateParams.id, fileId: previous.id });
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Open the file in a new window.
|
||||
*/
|
||||
$scope.openFile = function() {
|
||||
window.open('api/file/' + $scope.id + '/data');
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
dialog.open().then(function(result) {
|
||||
if (result == null) {
|
||||
$state.transitionTo('document.view', { id: $stateParams.id });
|
||||
}
|
||||
});
|
||||
});
|
@ -14,7 +14,7 @@
|
||||
<div class="control-group">
|
||||
<label class="control-label" for="inputFiles">New files</label>
|
||||
<div class="controls">
|
||||
<file class="input-block-level" id="inputFiles" multiple="multiple" ng-model="files" accept="image/png,image/jpg,image/jpeg,image/gif" />
|
||||
<file class="input-block-level" id="inputFiles" multiple="multiple" ng-model="newFiles" accept="image/png,image/jpg,image/jpeg,image/gif" />
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-actions">
|
||||
@ -22,3 +22,7 @@
|
||||
<button type="submit" class="btn" ng-click="cancel()">Cancel</button>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<div class="row-fluid">
|
||||
<div class="span12"><progress percent="fileProgress" class="progress-info active"></progress></div>
|
||||
</div>
|
@ -14,7 +14,7 @@
|
||||
<li class="span2" ng-repeat="file in files" ng-style="{ 'margin-left': $index % 6 == 0 ? '0' : '' }">
|
||||
<div class="thumbnail">
|
||||
<a ng-click="openFile(file)">
|
||||
<img ng-src="api/file/{{ file.id }}/data" tooltip="{{ file.mimetype }}" tooltip-placement="top" />
|
||||
<img ng-src="api/file/{{ file.id }}/data?thumbnail=true" tooltip="{{ file.mimetype }}" tooltip-placement="top" />
|
||||
</a>
|
||||
<div class="caption">
|
||||
<p class="text-right">
|
||||
@ -24,3 +24,5 @@
|
||||
</div>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<div ui-view="file"></div>
|
@ -3,5 +3,15 @@
|
||||
<button type="button" class="btn" ng-click="previousFile()">Previous</button>
|
||||
<button type="button" class="btn" ng-click="nextFile()">Next</button>
|
||||
</div>
|
||||
|
||||
<div class="btn-group pull-right">
|
||||
<button type="button" class="btn" ng-click="openFile()"><span class="icon-share"></span></button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
<img ng-show="file.mimetype == 'image/png' || file.mimetype == 'image/jpeg' || file.mimetype == 'image/gif'" ng-src="api/file/{{ id }}/data" />
|
||||
|
||||
<div class="text-center" ng-show="file.mimetype == 'application/pdf'">
|
||||
<img ng-src="api/file/{{ id }}/data?thumbnail=true" />
|
||||
</div>
|
||||
<img ng-src="api/file/{{ id }}/data" />
|
@ -76,16 +76,29 @@ public class TestFileResource extends BaseJerseyTest {
|
||||
// Get the file data
|
||||
fileResource = resource().path("/file/" + file1Id + "/data");
|
||||
fileResource.addFilter(new CookieAuthenticationFilter(file1AuthenticationToken));
|
||||
response = fileResource.get(ClientResponse.class);
|
||||
MultivaluedMapImpl getParams = new MultivaluedMapImpl();
|
||||
getParams.putSingle("thumbnail", false);
|
||||
response = fileResource.queryParams(getParams).get(ClientResponse.class);
|
||||
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
|
||||
InputStream is = response.getEntityInputStream();
|
||||
byte[] fileBytes = ByteStreams.toByteArray(is);
|
||||
Assert.assertEquals(163510, fileBytes.length);
|
||||
|
||||
// Get the thumbnail data
|
||||
fileResource = resource().path("/file/" + file1Id + "/data");
|
||||
fileResource.addFilter(new CookieAuthenticationFilter(file1AuthenticationToken));
|
||||
getParams = new MultivaluedMapImpl();
|
||||
getParams.putSingle("thumbnail", true);
|
||||
response = fileResource.queryParams(getParams).get(ClientResponse.class);
|
||||
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
|
||||
is = response.getEntityInputStream();
|
||||
fileBytes = ByteStreams.toByteArray(is);
|
||||
Assert.assertEquals(41935, fileBytes.length);
|
||||
|
||||
// Get all files from a document
|
||||
fileResource = resource().path("/file/list");
|
||||
fileResource.addFilter(new CookieAuthenticationFilter(file1AuthenticationToken));
|
||||
MultivaluedMapImpl getParams = new MultivaluedMapImpl();
|
||||
getParams = new MultivaluedMapImpl();
|
||||
getParams.putSingle("id", document1Id);
|
||||
response = fileResource.queryParams(getParams).get(ClientResponse.class);
|
||||
json = response.getEntity(JSONObject.class);
|
||||
|
Loading…
Reference in New Issue
Block a user