diff --git a/src/main/java/ru/ulstu/file/service/FileService.java b/src/main/java/ru/ulstu/file/service/FileService.java index 3a68bb6..68d0b35 100644 --- a/src/main/java/ru/ulstu/file/service/FileService.java +++ b/src/main/java/ru/ulstu/file/service/FileService.java @@ -13,6 +13,7 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.Date; import java.util.List; +import java.util.UUID; import java.util.stream.Collectors; import static java.nio.charset.StandardCharsets.UTF_8; @@ -39,7 +40,7 @@ public class FileService { } public String uploadToTmpDir(MultipartFile multipartFile) throws IOException { - String tmpFileName = String.valueOf(System.currentTimeMillis()); + String tmpFileName = String.valueOf(System.currentTimeMillis()) + UUID.randomUUID(); Files.write(getTmpFilePath(tmpFileName), multipartFile.getBytes()); String meta = multipartFile.getOriginalFilename() + "\n" + multipartFile.getSize(); Files.write(getTmpFileMetaPath(tmpFileName), meta.getBytes(UTF_8)); diff --git a/src/main/resources/public/js/core.js b/src/main/resources/public/js/core.js index 31642f4..216f411 100644 --- a/src/main/resources/public/js/core.js +++ b/src/main/resources/public/js/core.js @@ -2,6 +2,8 @@ /* global urlVersions */ var urlFileUpload = "/api/1.0/files/uploadTmpFile"; +var urlFileDownload = "/api/1.0/files/download/"; +var urlFileDownloadTmp = "/api/1.0/files/download-tmp/"; /* exported MessageTypesEnum */ var MessageTypesEnum = { diff --git a/src/main/resources/templates/papers/paper.html b/src/main/resources/templates/papers/paper.html index 996c991..cfba4bc 100644 --- a/src/main/resources/templates/papers/paper.html +++ b/src/main/resources/templates/papers/paper.html @@ -107,8 +107,17 @@ -
-
+
+ + +
+
+ +
@@ -251,12 +260,43 @@ var nameDiv = $("
") .addClass("col-10") - .append($("
").text(fileNames[1])); + .append($("").text(fileNames[1]) + .attr("href", 'javascript:void(0)') + .attr("onclick", "downloadFile('" + fileNames[0] + "',null,'" + fileNames[1] + "')")); newFileRow.append(nameDiv); $("#files-list").append(newFileRow); } + + function downloadFile(tmpName, fileId, downloadName) { + let xhr = new XMLHttpRequest(); + if (fileId != null) xhr.open('GET', urlFileDownload + fileId); + if (tmpName != null) xhr.open('GET', urlFileDownloadTmp + tmpName); + xhr.responseType = 'blob'; + + var formData = new FormData(); + if (fileId != null) formData.append("file-id", fileId); + if (tmpName != null) formData.append("tmp-file-name", tmpName); + + xhr.send(formData); + + xhr.onload = function () { + if (this.status == 200) { + console.debug(this.response); + var blob = new Blob([this.response], {type: '*'}); + let a = document.createElement("a"); + a.style = "display: none"; + document.body.appendChild(a); + let url = window.URL.createObjectURL(blob); + a.href = url; + a.download = downloadName; + a.click(); + window.URL.revokeObjectURL(url); + } else { + } + } + }