Closes #208: display file content + fix filename encoding

This commit is contained in:
Benjamin Gamard 2018-03-23 16:41:02 +01:00
parent 55a4bb7621
commit be1c2a7b90
10 changed files with 53 additions and 12 deletions

View File

@ -54,6 +54,7 @@ public class FileCreatedAsyncListener {
FormatHandler formatHandler = FormatHandlerUtil.find(file.getMimeType());
if (formatHandler == null) {
log.error("Format unhandled: " + file.getMimeType());
FileUtil.endProcessingFile(file.getId());
return;
}
@ -65,6 +66,7 @@ public class FileCreatedAsyncListener {
});
if (user.get() == null) {
// The user has been deleted meanwhile
FileUtil.endProcessingFile(file.getId());
return;
}

View File

@ -40,6 +40,7 @@ import javax.ws.rs.core.StreamingOutput;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.net.URLDecoder;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
@ -119,7 +120,7 @@ public class FileResource extends BaseResource {
try {
String name = fileBodyPart.getContentDisposition() != null ?
fileBodyPart.getContentDisposition().getFileName() : null;
URLDecoder.decode(fileBodyPart.getContentDisposition().getFileName(), "UTF-8") : null;
String fileId = FileUtil.createFile(name, unencryptedFile, fileSize, documentDto == null ?
null : documentDto.getLanguage(), principal.getId(), documentId);
@ -449,7 +450,7 @@ public class FileResource extends BaseResource {
* @apiGroup File
* @apiParam {String} id File ID
* @apiParam {String} share Share ID
* @apiParam {String="web","thumb"} [size] Size variation
* @apiParam {String="web","thumb","content"} [size] Size variation
* @apiSuccess {Object} file The file data is the whole response
* @apiError (client) SizeError Size must be web or thumb
* @apiError (client) ForbiddenError Access denied or document not visible
@ -469,12 +470,10 @@ public class FileResource extends BaseResource {
@QueryParam("size") String size) {
authenticate();
if (size != null) {
if (!Lists.newArrayList("web", "thumb").contains(size)) {
throw new ClientException("SizeError", "Size must be web or thumb");
}
if (size != null && !Lists.newArrayList("web", "thumb", "content").contains(size)) {
throw new ClientException("SizeError", "Size must be web, thumb or content");
}
// Get the file
File file = findFile(fileId, shareId);
@ -484,6 +483,12 @@ public class FileResource extends BaseResource {
String mimeType;
boolean decrypt;
if (size != null) {
if (size.equals("content")) {
return Response.ok(Strings.nullToEmpty(file.getContent()))
.header(HttpHeaders.CONTENT_TYPE, "text/plain")
.build();
}
storedFile = DirectoryUtil.getStorageDirectory().resolve(fileId + "_" + size);
mimeType = MimeType.IMAGE_JPEG; // Thumbnails are JPEG
decrypt = true; // Thumbnails are encrypted

View File

@ -153,7 +153,7 @@ angular.module('docs').controller('DocumentEdit', function($rootScope, $scope, $
var file = $scope.newFiles[key];
var formData = new FormData();
formData.append('id', data.id);
formData.append('file', file, file.name);
formData.append('file', file, encodeURIComponent(file.name));
// Send the file
$.ajax({

View File

@ -70,6 +70,13 @@ angular.module('docs').controller('FileModalView', function ($uibModalInstance,
window.open('../api/file/' + $stateParams.fileId + '/data');
};
/**
* Open the file content a new window.
*/
$scope.openFileContent = function () {
window.open('../api/file/' + $stateParams.fileId + '/data?size=content');
};
/**
* Print the file.
*/

View File

@ -1,4 +1,8 @@
/**!
* =======================================================================
* Sismics Docs patch applied to encode filenames with encodeURIComponent.
* =======================================================================
*
* AngularJS file upload directives and services. Supoorts: file upload/drop/paste, resume, cancel/abort,
* progress, resize, thumbnail, preview, validation and CORS
* @author Danial <danial.farid@gmail.com>
@ -272,7 +276,7 @@ ngFileUpload.service('UploadBase', ['$http', '$q', '$timeout', function ($http,
key = split[0];
}
config._fileKey = config._fileKey || key;
formData.append(key, file, file.ngfName || file.name);
formData.append(key, file, file.ngfName || encodeURIComponent(file.name));
} else {
if (angular.isObject(val)) {
if (val.$$ngfCircularDetection) throw 'ngFileUpload: Circular reference in config.data. Make sure specified data for Upload.upload() has no circular reference: ' + key;

View File

@ -60,7 +60,7 @@
<div class="btn btn-default handle"><span class="fas fa-arrows-alt-h"></span></div>
</div>
<div class="v-align">
<div class="v-align" ng-show="document.writable">
<div uib-dropdown>
<button class="btn btn-default" uib-dropdown-toggle>
<span class="fas fa-ellipsis-v"></span>

View File

@ -22,12 +22,15 @@
</div>
<div class="btn-group pull-right">
<button type="button" class="btn btn-default" ng-click="printFile()">
<button type="button" class="btn btn-default" uib-tooltip="Print this file" tooltip-append-to-body="true" ng-click="printFile()">
<span class="fas fa-print"></span>
</button>
<button type="button" class="btn btn-default" ng-click="openFile()">
<button type="button" class="btn btn-default" uib-tooltip="Download this file" tooltip-append-to-body="true" ng-click="openFile()">
<span class="fas fa-download"></span>
</button>
<button type="button" class="btn btn-default" uib-tooltip="Show text content" tooltip-append-to-body="true" ng-click="openFileContent()">
<span class="fas fa-eye"></span>
</button>
</div>
</div>

View File

@ -429,6 +429,8 @@ input[readonly].share-link {
}
.settings-content {
margin-bottom: 10px;
.well {
box-shadow: 0 7px 14px 0 rgba(50,50,93,.1), 0 3px 6px 0 rgba(0,0,0,.07);
background: none;

View File

@ -580,6 +580,16 @@ public class TestDocumentResource extends BaseJerseyTest {
Assert.assertTrue(fileBytes.length > 0); // Images rendered from PDF differ in size from OS to OS due to font issues
Assert.assertEquals(MimeType.IMAGE_JPEG, MimeTypeUtil.guessMimeType(fileBytes, null));
// Get the content data
response = target().path("/file/" + file1Id + "/data")
.queryParam("size", "content")
.request()
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, documentPlainToken)
.get();
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
is = (InputStream) response.getEntity();
Assert.assertTrue(new String(ByteStreams.toByteArray(is)).contains("love"));
// Export a document in PDF format
response = target().path("/document/" + document1Id + "/pdf")
.queryParam("margin", "10")

View File

@ -106,6 +106,14 @@ public class TestFileResource extends BaseJerseyTest {
Assert.assertEquals(MimeType.IMAGE_JPEG, MimeTypeUtil.guessMimeType(fileBytes, null));
Assert.assertTrue(fileBytes.length > 0);
// Get the content data
response = target().path("/file/" + file1Id + "/data")
.queryParam("size", "content")
.request()
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, file1Token)
.get();
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
// Get the web data
response = target().path("/file/" + file1Id + "/data")
.queryParam("size", "web")