chunking
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package com.gregor_lohaus.gtransfer.controller;
|
package com.gregor_lohaus.gtransfer.controller;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@@ -12,11 +13,13 @@ import org.springframework.stereotype.Controller;
|
|||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.ResponseBody;
|
import org.springframework.web.bind.annotation.ResponseBody;
|
||||||
|
|
||||||
import com.gregor_lohaus.gtransfer.model.File;
|
import com.gregor_lohaus.gtransfer.model.File;
|
||||||
import com.gregor_lohaus.gtransfer.model.FileRepository;
|
import com.gregor_lohaus.gtransfer.model.FileRepository;
|
||||||
import com.gregor_lohaus.gtransfer.services.filewriter.AbstractStorageService;
|
import com.gregor_lohaus.gtransfer.services.filewriter.AbstractStorageService;
|
||||||
|
import com.gregor_lohaus.gtransfer.services.filewriter.StorageKeys;
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
public class DownloadController {
|
public class DownloadController {
|
||||||
@@ -32,28 +35,78 @@ public class DownloadController {
|
|||||||
return "download/page";
|
return "download/page";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("/download/{id}/metadata")
|
||||||
|
@ResponseBody
|
||||||
|
@Transactional
|
||||||
|
public ResponseEntity<Map<String, Object>> metadata(@PathVariable String id) {
|
||||||
|
AvailableFile available = getAvailableFile(id);
|
||||||
|
if (!available.found()) {
|
||||||
|
return ResponseEntity.status(available.status()).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
File file = available.file();
|
||||||
|
return ResponseEntity.ok(Map.of(
|
||||||
|
"name", file.getName(),
|
||||||
|
"chunkCount", file.getChunkCount(),
|
||||||
|
"size", file.getSize() == null ? 0 : file.getSize()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/download/{id}/chunk/{index}")
|
||||||
|
@ResponseBody
|
||||||
|
@Transactional
|
||||||
|
public ResponseEntity<byte[]> chunk(@PathVariable String id, @PathVariable Integer index) {
|
||||||
|
AvailableFile available = getAvailableFile(id);
|
||||||
|
if (!available.found()) {
|
||||||
|
return ResponseEntity.status(available.status()).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
File file = available.file();
|
||||||
|
if (index == null || index < 0 || index >= file.getChunkCount()) {
|
||||||
|
return ResponseEntity.badRequest().build();
|
||||||
|
}
|
||||||
|
|
||||||
|
Optional<byte[]> data = storageService.get(storageKey(file, index));
|
||||||
|
if (data.isEmpty()) {
|
||||||
|
return ResponseEntity.notFound().build();
|
||||||
|
}
|
||||||
|
|
||||||
|
return ResponseEntity.ok()
|
||||||
|
.contentType(MediaType.APPLICATION_OCTET_STREAM)
|
||||||
|
.body(data.get());
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/download/{id}/complete")
|
||||||
|
@ResponseBody
|
||||||
|
@Transactional
|
||||||
|
public ResponseEntity<Void> complete(@PathVariable String id) {
|
||||||
|
AvailableFile available = getAvailableFile(id);
|
||||||
|
if (!available.found()) {
|
||||||
|
return ResponseEntity.status(available.status()).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
File file = available.file();
|
||||||
|
file.setDownloads(file.getDownloads() + 1);
|
||||||
|
fileRepository.save(file);
|
||||||
|
|
||||||
|
if (file.getDownloadLimit() != null && file.getDownloads() >= file.getDownloadLimit()) {
|
||||||
|
deleteStoredFile(file);
|
||||||
|
fileRepository.delete(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ResponseEntity.noContent().build();
|
||||||
|
}
|
||||||
|
|
||||||
@GetMapping("/download/{id}/data")
|
@GetMapping("/download/{id}/data")
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
@Transactional
|
@Transactional
|
||||||
public ResponseEntity<byte[]> data(@PathVariable String id) {
|
public ResponseEntity<byte[]> data(@PathVariable String id) {
|
||||||
Optional<File> fileOpt = fileRepository.findById(id);
|
AvailableFile available = getAvailableFile(id);
|
||||||
if (fileOpt.isEmpty()) {
|
if (!available.found()) {
|
||||||
return ResponseEntity.notFound().build();
|
return ResponseEntity.status(available.status()).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
File file = fileOpt.get();
|
File file = available.file();
|
||||||
|
if (file.isChunked()) {
|
||||||
// Check expiry
|
|
||||||
if (file.getExpireyDateTime() != null && LocalDateTime.now().isAfter(file.getExpireyDateTime())) {
|
|
||||||
storageService.delete(id);
|
|
||||||
fileRepository.delete(file);
|
|
||||||
return ResponseEntity.status(HttpStatus.GONE).build();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check download limit before serving
|
|
||||||
if (file.getDownloadLimit() != null && file.getDownloads() >= file.getDownloadLimit()) {
|
|
||||||
storageService.delete(id);
|
|
||||||
fileRepository.delete(file);
|
|
||||||
return ResponseEntity.status(HttpStatus.GONE).build();
|
return ResponseEntity.status(HttpStatus.GONE).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,22 +115,80 @@ public class DownloadController {
|
|||||||
return ResponseEntity.notFound().build();
|
return ResponseEntity.notFound().build();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Increment counter
|
|
||||||
file.setDownloads(file.getDownloads() + 1);
|
file.setDownloads(file.getDownloads() + 1);
|
||||||
fileRepository.save(file);
|
fileRepository.save(file);
|
||||||
|
|
||||||
// Clean up if limit now reached
|
|
||||||
if (file.getDownloadLimit() != null && file.getDownloads() >= file.getDownloadLimit()) {
|
if (file.getDownloadLimit() != null && file.getDownloads() >= file.getDownloadLimit()) {
|
||||||
storageService.delete(id);
|
deleteStoredFile(file);
|
||||||
fileRepository.delete(file);
|
fileRepository.delete(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
String disposition = "attachment; filename=\""
|
|
||||||
+ file.getName().replace("\\", "\\\\").replace("\"", "\\\"") + "\"";
|
|
||||||
|
|
||||||
return ResponseEntity.ok()
|
return ResponseEntity.ok()
|
||||||
.header(HttpHeaders.CONTENT_DISPOSITION, disposition)
|
.header(HttpHeaders.CONTENT_DISPOSITION, contentDisposition(file.getName()))
|
||||||
.contentType(MediaType.APPLICATION_OCTET_STREAM)
|
.contentType(MediaType.APPLICATION_OCTET_STREAM)
|
||||||
.body(data.get());
|
.body(data.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private AvailableFile getAvailableFile(String id) {
|
||||||
|
Optional<File> fileOpt = fileRepository.findById(id);
|
||||||
|
if (fileOpt.isEmpty()) {
|
||||||
|
return AvailableFile.notFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
File file = fileOpt.get();
|
||||||
|
if (file.getExpireyDateTime() != null && LocalDateTime.now().isAfter(file.getExpireyDateTime())) {
|
||||||
|
deleteStoredFile(file);
|
||||||
|
fileRepository.delete(file);
|
||||||
|
return AvailableFile.gone();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (file.getDownloadLimit() != null && file.getDownloads() >= file.getDownloadLimit()) {
|
||||||
|
deleteStoredFile(file);
|
||||||
|
fileRepository.delete(file);
|
||||||
|
return AvailableFile.gone();
|
||||||
|
}
|
||||||
|
|
||||||
|
return AvailableFile.ok(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void deleteStoredFile(File file) {
|
||||||
|
if (!file.isChunked()) {
|
||||||
|
storageService.delete(file.getId());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < file.getChunkCount(); i++) {
|
||||||
|
storageService.delete(StorageKeys.chunk(file.getId(), i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String storageKey(File file, int index) {
|
||||||
|
if (!file.isChunked()) {
|
||||||
|
return file.getId();
|
||||||
|
}
|
||||||
|
return StorageKeys.chunk(file.getId(), index);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String contentDisposition(String filename) {
|
||||||
|
return "attachment; filename=\""
|
||||||
|
+ filename.replace("\\", "\\\\").replace("\"", "\\\"") + "\"";
|
||||||
|
}
|
||||||
|
|
||||||
|
private record AvailableFile(File file, HttpStatus status) {
|
||||||
|
static AvailableFile ok(File file) {
|
||||||
|
return new AvailableFile(file, HttpStatus.OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
static AvailableFile notFound() {
|
||||||
|
return new AvailableFile(null, HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
|
|
||||||
|
static AvailableFile gone() {
|
||||||
|
return new AvailableFile(null, HttpStatus.GONE);
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean found() {
|
||||||
|
return file != null;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,11 +2,15 @@ package com.gregor_lohaus.gtransfer.controller;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.OptionalLong;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.ui.Model;
|
import org.springframework.ui.Model;
|
||||||
|
import org.springframework.web.server.ResponseStatusException;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
@@ -15,6 +19,7 @@ import org.springframework.web.multipart.MultipartFile;
|
|||||||
import com.gregor_lohaus.gtransfer.model.File;
|
import com.gregor_lohaus.gtransfer.model.File;
|
||||||
import com.gregor_lohaus.gtransfer.model.FileRepository;
|
import com.gregor_lohaus.gtransfer.model.FileRepository;
|
||||||
import com.gregor_lohaus.gtransfer.services.filewriter.AbstractStorageService;
|
import com.gregor_lohaus.gtransfer.services.filewriter.AbstractStorageService;
|
||||||
|
import com.gregor_lohaus.gtransfer.services.filewriter.StorageKeys;
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
public class UploadController {
|
public class UploadController {
|
||||||
@@ -41,21 +46,46 @@ public class UploadController {
|
|||||||
|
|
||||||
@PostMapping("/upload")
|
@PostMapping("/upload")
|
||||||
public String upload(
|
public String upload(
|
||||||
@RequestParam("file") MultipartFile file,
|
|
||||||
@RequestParam("hash") String hash,
|
@RequestParam("hash") String hash,
|
||||||
@RequestParam("name") String name,
|
@RequestParam("name") String name,
|
||||||
|
@RequestParam("chunkCount") Integer chunkCount,
|
||||||
|
@RequestParam("size") Long size,
|
||||||
@RequestParam(required = false) Integer expiryDays,
|
@RequestParam(required = false) Integer expiryDays,
|
||||||
@RequestParam(required = false) Integer downloadLimit) throws IOException {
|
@RequestParam(required = false) Integer downloadLimit) {
|
||||||
|
if (!isValidId(hash) || chunkCount == null || chunkCount < 1 || size == null || size < 0) {
|
||||||
storageService.put(hash, file.getBytes());
|
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Invalid upload metadata");
|
||||||
|
}
|
||||||
|
|
||||||
int days = expiryDays != null ? Math.min(expiryDays, maxExpiryDays) : maxExpiryDays;
|
int days = expiryDays != null ? Math.min(expiryDays, maxExpiryDays) : maxExpiryDays;
|
||||||
Integer limit = downloadLimit != null ? Math.min(downloadLimit, maxDownloadLimit) : null;
|
Integer limit = downloadLimit != null ? Math.min(downloadLimit, maxDownloadLimit) : null;
|
||||||
|
|
||||||
File f = new File(hash, hash, name, LocalDateTime.now().plusDays(days));
|
File f = new File(hash, hash, name, LocalDateTime.now().plusDays(days));
|
||||||
|
f.setChunkCount(chunkCount);
|
||||||
|
f.setSize(size);
|
||||||
f.setDownloadLimit(limit);
|
f.setDownloadLimit(limit);
|
||||||
fileRepository.save(f);
|
fileRepository.save(f);
|
||||||
|
|
||||||
return "upload/result :: view";
|
return "upload/result :: view";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PostMapping("/upload/chunk")
|
||||||
|
public ResponseEntity<Void> uploadChunk(
|
||||||
|
@RequestParam("chunk") MultipartFile chunk,
|
||||||
|
@RequestParam("hash") String hash,
|
||||||
|
@RequestParam("index") Integer index) throws IOException {
|
||||||
|
if (!isValidId(hash) || index == null || index < 0) {
|
||||||
|
return ResponseEntity.badRequest().build();
|
||||||
|
}
|
||||||
|
|
||||||
|
OptionalLong written = storageService.put(StorageKeys.chunk(hash, index), chunk.getBytes());
|
||||||
|
if (written.isEmpty()) {
|
||||||
|
return ResponseEntity.internalServerError().build();
|
||||||
|
}
|
||||||
|
|
||||||
|
return ResponseEntity.noContent().build();
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isValidId(String id) {
|
||||||
|
return id != null && id.matches("[a-f0-9]{64}");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,8 @@ public class File {
|
|||||||
private String id;
|
private String id;
|
||||||
private String path;
|
private String path;
|
||||||
private String name;
|
private String name;
|
||||||
|
private Integer chunkCount;
|
||||||
|
private Long size;
|
||||||
private LocalDateTime expireyDateTime;
|
private LocalDateTime expireyDateTime;
|
||||||
private Integer downloadLimit;
|
private Integer downloadLimit;
|
||||||
@Column(columnDefinition = "integer default 0")
|
@Column(columnDefinition = "integer default 0")
|
||||||
@@ -41,6 +43,21 @@ public class File {
|
|||||||
public void setName(String name) {
|
public void setName(String name) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
public Integer getChunkCount() {
|
||||||
|
return chunkCount == null ? 1 : chunkCount;
|
||||||
|
}
|
||||||
|
public void setChunkCount(Integer chunkCount) {
|
||||||
|
this.chunkCount = chunkCount;
|
||||||
|
}
|
||||||
|
public boolean isChunked() {
|
||||||
|
return chunkCount != null;
|
||||||
|
}
|
||||||
|
public Long getSize() {
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
public void setSize(Long size) {
|
||||||
|
this.size = size;
|
||||||
|
}
|
||||||
public Integer getDownloadLimit() {
|
public Integer getDownloadLimit() {
|
||||||
return downloadLimit;
|
return downloadLimit;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import org.springframework.transaction.annotation.Transactional;
|
|||||||
import com.gregor_lohaus.gtransfer.model.File;
|
import com.gregor_lohaus.gtransfer.model.File;
|
||||||
import com.gregor_lohaus.gtransfer.model.FileRepository;
|
import com.gregor_lohaus.gtransfer.model.FileRepository;
|
||||||
import com.gregor_lohaus.gtransfer.services.filewriter.AbstractStorageService;
|
import com.gregor_lohaus.gtransfer.services.filewriter.AbstractStorageService;
|
||||||
|
import com.gregor_lohaus.gtransfer.services.filewriter.StorageKeys;
|
||||||
|
|
||||||
public class FileCleanupService {
|
public class FileCleanupService {
|
||||||
private Boolean enabled;
|
private Boolean enabled;
|
||||||
@@ -43,9 +44,20 @@ public class FileCleanupService {
|
|||||||
};
|
};
|
||||||
|
|
||||||
for (File file : expired) {
|
for (File file : expired) {
|
||||||
storageService.delete(file.getId());
|
deleteStoredFile(file);
|
||||||
fileRepository.delete(file);
|
fileRepository.delete(file);
|
||||||
}
|
}
|
||||||
log.info("Cleaned up {} expired file(s)", expired.size());
|
log.info("Cleaned up {} expired file(s)", expired.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void deleteStoredFile(File file) {
|
||||||
|
if (!file.isChunked()) {
|
||||||
|
storageService.delete(file.getId());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < file.getChunkCount(); i++) {
|
||||||
|
storageService.delete(StorageKeys.chunk(file.getId(), i));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,8 +15,12 @@ public class LocalStorageService extends AbstractStorageService {
|
|||||||
@Override
|
@Override
|
||||||
public OptionalLong put(String id, byte[] data) {
|
public OptionalLong put(String id, byte[] data) {
|
||||||
try {
|
try {
|
||||||
Files.createDirectories(root);
|
Path target = root.resolve(id);
|
||||||
Files.write(root.resolve(id), data);
|
Path parent = target.getParent();
|
||||||
|
if (parent != null) {
|
||||||
|
Files.createDirectories(parent);
|
||||||
|
}
|
||||||
|
Files.write(target, data);
|
||||||
return OptionalLong.of(data.length);
|
return OptionalLong.of(data.length);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
return OptionalLong.empty();
|
return OptionalLong.empty();
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package com.gregor_lohaus.gtransfer.services.filewriter;
|
||||||
|
|
||||||
|
public final class StorageKeys {
|
||||||
|
private StorageKeys() {}
|
||||||
|
|
||||||
|
public static String chunk(String id, int index) {
|
||||||
|
return id + "/chunks/" + index;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,3 +1,33 @@
|
|||||||
|
var DEFAULT_CHUNK_SIZE = 4 * 1024 * 1024;
|
||||||
|
|
||||||
|
async function generateFileKey() {
|
||||||
|
const key = await crypto.subtle.generateKey(
|
||||||
|
{ name: 'AES-GCM', length: 256 },
|
||||||
|
true,
|
||||||
|
['encrypt', 'decrypt']
|
||||||
|
);
|
||||||
|
|
||||||
|
const rawKey = await crypto.subtle.exportKey('raw', key);
|
||||||
|
const hash = await hashKey(rawKey);
|
||||||
|
const base64urlKey = encodeKey(rawKey);
|
||||||
|
|
||||||
|
return { key, rawKey, hash, base64urlKey };
|
||||||
|
}
|
||||||
|
|
||||||
|
async function encryptFileChunk(file, index, key, chunkSize = DEFAULT_CHUNK_SIZE) {
|
||||||
|
const start = index * chunkSize;
|
||||||
|
const end = Math.min(start + chunkSize, file.size);
|
||||||
|
const plaintext = await file.slice(start, end).arrayBuffer();
|
||||||
|
const iv = crypto.getRandomValues(new Uint8Array(12));
|
||||||
|
const ciphertext = await crypto.subtle.encrypt({ name: 'AES-GCM', iv }, key, plaintext);
|
||||||
|
|
||||||
|
const payload = new Uint8Array(12 + ciphertext.byteLength);
|
||||||
|
payload.set(iv, 0);
|
||||||
|
payload.set(new Uint8Array(ciphertext), 12);
|
||||||
|
|
||||||
|
return payload;
|
||||||
|
}
|
||||||
|
|
||||||
async function encryptFile(arrayBuffer) {
|
async function encryptFile(arrayBuffer) {
|
||||||
const key = await crypto.subtle.generateKey(
|
const key = await crypto.subtle.generateKey(
|
||||||
{ name: 'AES-GCM', length: 256 },
|
{ name: 'AES-GCM', length: 256 },
|
||||||
@@ -30,6 +60,10 @@ async function hashKey(rawKey) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function decryptFile(payload, key) {
|
async function decryptFile(payload, key) {
|
||||||
|
return decryptChunk(payload, key);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function decryptChunk(payload, key) {
|
||||||
const bytes = new Uint8Array(payload);
|
const bytes = new Uint8Array(payload);
|
||||||
const iv = bytes.slice(0, 12);
|
const iv = bytes.slice(0, 12);
|
||||||
const ciphertext = bytes.slice(12);
|
const ciphertext = bytes.slice(12);
|
||||||
|
|||||||
@@ -11,24 +11,46 @@ if (!fragment) {
|
|||||||
'raw', rawKeyBytes, { name: 'AES-GCM' }, false, ['decrypt']
|
'raw', rawKeyBytes, { name: 'AES-GCM' }, false, ['decrypt']
|
||||||
);
|
);
|
||||||
|
|
||||||
setStatus('Downloading\u2026');
|
setProgress('Loading metadata\u2026', 0, 1);
|
||||||
const response = await fetch('/download/' + id + '/data');
|
const metadataResponse = await fetch('/download/' + id + '/metadata');
|
||||||
|
|
||||||
if (response.status === 410) {
|
if (metadataResponse.status === 410) {
|
||||||
showError('This file has expired or reached its download limit.');
|
showError('This file has expired or reached its download limit.');
|
||||||
} else if (!response.ok) {
|
} else if (!metadataResponse.ok) {
|
||||||
showError(`Download failed (${response.status}).`);
|
showError(`Download failed (${metadataResponse.status}).`);
|
||||||
} else {
|
} else {
|
||||||
const disposition = response.headers.get('Content-Disposition') || '';
|
const metadata = await metadataResponse.json();
|
||||||
const filename = disposition.match(/filename="?([^"]+)"?/)?.[1] || 'download';
|
const filename = metadata.name || 'download';
|
||||||
|
const chunkCount = metadata.chunkCount || 1;
|
||||||
|
const chunks = [];
|
||||||
|
|
||||||
setStatus('Decrypting\u2026');
|
for (let index = 0; index < chunkCount; index++) {
|
||||||
const plaintext = await decryptFile(await response.arrayBuffer(), key);
|
setProgress(`Downloading chunk ${index + 1} of ${chunkCount}\u2026`, index, chunkCount);
|
||||||
|
const chunkResponse = await fetch('/download/' + id + '/chunk/' + index);
|
||||||
|
if (chunkResponse.status === 410) {
|
||||||
|
throw new Error('This file has expired or reached its download limit.');
|
||||||
|
}
|
||||||
|
if (!chunkResponse.ok) {
|
||||||
|
throw new Error(`Chunk download failed (${chunkResponse.status})`);
|
||||||
|
}
|
||||||
|
|
||||||
|
setProgress(`Decrypting chunk ${index + 1} of ${chunkCount}\u2026`, index + 0.5, chunkCount);
|
||||||
|
const plaintextChunk = await decryptChunk(await chunkResponse.arrayBuffer(), key);
|
||||||
|
chunks.push(new Uint8Array(plaintextChunk));
|
||||||
|
setProgress(`Downloaded ${index + 1} of ${chunkCount} chunks`, index + 1, chunkCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
const completeResponse = await fetch('/download/' + id + '/complete', { method: 'POST' });
|
||||||
|
if (!completeResponse.ok && completeResponse.status !== 404 && completeResponse.status !== 410) {
|
||||||
|
throw new Error(`Download completion failed (${completeResponse.status})`);
|
||||||
|
}
|
||||||
|
|
||||||
|
setProgress('Preparing preview\u2026', chunkCount, chunkCount);
|
||||||
|
const plaintext = combineChunks(chunks);
|
||||||
showPreview(filename, plaintext);
|
showPreview(filename, plaintext);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
showError('Decryption failed: ' + err.message);
|
showError(err.message);
|
||||||
}
|
}
|
||||||
|
|
||||||
function setStatus(msg) {
|
function setStatus(msg) {
|
||||||
@@ -36,6 +58,16 @@ function setStatus(msg) {
|
|||||||
if (el) el.textContent = msg;
|
if (el) el.textContent = msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function setProgress(msg, completed, total) {
|
||||||
|
setStatus(msg);
|
||||||
|
const percent = Math.round((completed / total) * 100);
|
||||||
|
const bar = htmx.find('#download-progress');
|
||||||
|
if (!bar) return;
|
||||||
|
bar.style.width = `${percent}%`;
|
||||||
|
bar.textContent = `${percent}%`;
|
||||||
|
bar.setAttribute('aria-valuenow', String(percent));
|
||||||
|
}
|
||||||
|
|
||||||
function escapeHtml(str) {
|
function escapeHtml(str) {
|
||||||
return str.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
|
return str.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
|
||||||
}
|
}
|
||||||
@@ -58,6 +90,17 @@ function getMimeType(filename) {
|
|||||||
return types[ext] || 'application/octet-stream';
|
return types[ext] || 'application/octet-stream';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function combineChunks(chunks) {
|
||||||
|
const size = chunks.reduce((sum, chunk) => sum + chunk.byteLength, 0);
|
||||||
|
const combined = new Uint8Array(size);
|
||||||
|
let offset = 0;
|
||||||
|
for (const chunk of chunks) {
|
||||||
|
combined.set(chunk, offset);
|
||||||
|
offset += chunk.byteLength;
|
||||||
|
}
|
||||||
|
return combined.buffer;
|
||||||
|
}
|
||||||
|
|
||||||
function showPreview(filename, plaintext) {
|
function showPreview(filename, plaintext) {
|
||||||
const mimeType = getMimeType(filename);
|
const mimeType = getMimeType(filename);
|
||||||
const blob = new Blob([plaintext], { type: mimeType });
|
const blob = new Blob([plaintext], { type: mimeType });
|
||||||
|
|||||||
@@ -43,22 +43,41 @@ async function startUpload() {
|
|||||||
<span class="visually-hidden">Loading\u2026</span>
|
<span class="visually-hidden">Loading\u2026</span>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="drop-zone-text" id="upload-status">Encrypting\u2026</div>`,
|
<div class="drop-zone-text mb-3" id="upload-status">Preparing\u2026</div>
|
||||||
|
<div class="progress" role="progressbar" aria-label="Upload progress" aria-valuemin="0" aria-valuemax="100">
|
||||||
|
<div id="upload-progress" class="progress-bar bg-success" style="width: 0%">0%</div>
|
||||||
|
</div>`,
|
||||||
{ swapStyle: 'innerHTML' });
|
{ swapStyle: 'innerHTML' });
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const { payload, hash, base64urlKey } = await encryptFile(await selectedFile.arrayBuffer());
|
const { key, hash, base64urlKey } = await generateFileKey();
|
||||||
|
const chunkCount = Math.max(1, Math.ceil(selectedFile.size / DEFAULT_CHUNK_SIZE));
|
||||||
|
|
||||||
setStatus('Uploading\u2026');
|
for (let index = 0; index < chunkCount; index++) {
|
||||||
|
setProgress(`Encrypting chunk ${index + 1} of ${chunkCount}\u2026`, index, chunkCount);
|
||||||
|
const payload = await encryptFileChunk(selectedFile, index, key);
|
||||||
|
|
||||||
const formData = new FormData();
|
setProgress(`Uploading chunk ${index + 1} of ${chunkCount}\u2026`, index + 0.5, chunkCount);
|
||||||
formData.append('file', new Blob([payload]), selectedFile.name);
|
const chunkData = new FormData();
|
||||||
formData.append('hash', hash);
|
chunkData.append('chunk', new Blob([payload]), String(index));
|
||||||
formData.append('name', selectedFile.name);
|
chunkData.append('hash', hash);
|
||||||
if (expiryDays) formData.append('expiryDays', expiryDays);
|
chunkData.append('index', index);
|
||||||
if (downloadLimit) formData.append('downloadLimit', downloadLimit);
|
|
||||||
|
|
||||||
const response = await fetch('/upload', { method: 'POST', body: formData });
|
const chunkResponse = await fetch('/upload/chunk', { method: 'POST', body: chunkData });
|
||||||
|
if (!chunkResponse.ok) throw new Error(`Chunk upload failed (${chunkResponse.status})`);
|
||||||
|
setProgress(`Uploaded chunk ${index + 1} of ${chunkCount}`, index + 1, chunkCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
setProgress('Finalizing\u2026', chunkCount, chunkCount);
|
||||||
|
const metadata = new FormData();
|
||||||
|
metadata.append('hash', hash);
|
||||||
|
metadata.append('name', selectedFile.name);
|
||||||
|
metadata.append('chunkCount', chunkCount);
|
||||||
|
metadata.append('size', selectedFile.size);
|
||||||
|
if (expiryDays) metadata.append('expiryDays', expiryDays);
|
||||||
|
if (downloadLimit) metadata.append('downloadLimit', downloadLimit);
|
||||||
|
|
||||||
|
const response = await fetch('/upload', { method: 'POST', body: metadata });
|
||||||
if (!response.ok) throw new Error(`Server error ${response.status}`);
|
if (!response.ok) throw new Error(`Server error ${response.status}`);
|
||||||
|
|
||||||
htmx.swap(dropZone, await response.text(), { swapStyle: 'innerHTML' });
|
htmx.swap(dropZone, await response.text(), { swapStyle: 'innerHTML' });
|
||||||
@@ -79,6 +98,16 @@ function setStatus(msg) {
|
|||||||
if (el) el.textContent = msg;
|
if (el) el.textContent = msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function setProgress(msg, completed, total) {
|
||||||
|
setStatus(msg);
|
||||||
|
const percent = Math.round((completed / total) * 100);
|
||||||
|
const bar = htmx.find('#upload-progress');
|
||||||
|
if (!bar) return;
|
||||||
|
bar.style.width = `${percent}%`;
|
||||||
|
bar.textContent = `${percent}%`;
|
||||||
|
bar.setAttribute('aria-valuenow', String(percent));
|
||||||
|
}
|
||||||
|
|
||||||
function resetUpload() {
|
function resetUpload() {
|
||||||
selectedFile = null;
|
selectedFile = null;
|
||||||
fileInput.value = '';
|
fileInput.value = '';
|
||||||
|
|||||||
@@ -20,7 +20,10 @@
|
|||||||
<main class="flex-grow-1 d-flex align-items-center justify-content-center py-5 px-3">
|
<main class="flex-grow-1 d-flex align-items-center justify-content-center py-5 px-3">
|
||||||
<div id="download-state" class="drop-zone text-center py-5 px-4" style="max-width: 520px; width: 100%;">
|
<div id="download-state" class="drop-zone text-center py-5 px-4" style="max-width: 520px; width: 100%;">
|
||||||
<div class="drop-zone-icon mb-3">🔒</div>
|
<div class="drop-zone-icon mb-3">🔒</div>
|
||||||
<div class="drop-zone-text" id="download-status">Preparing…</div>
|
<div class="drop-zone-text mb-3" id="download-status">Preparing…</div>
|
||||||
|
<div class="progress" role="progressbar" aria-label="Download progress" aria-valuemin="0" aria-valuemax="100">
|
||||||
|
<div id="download-progress" class="progress-bar bg-success" style="width: 0%">0%</div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</main>
|
</main>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user