Merge branch 'chunked'
All checks were successful
Deploy / Explore-Gitea-Actions (push) Successful in 8m45s

This commit is contained in:
2026-06-05 14:50:13 +02:00
10 changed files with 275 additions and 75 deletions

View File

@@ -1,10 +1,10 @@
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;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
@@ -12,11 +12,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,52 +34,109 @@ public class DownloadController {
return "download/page"; return "download/page";
} }
@GetMapping("/download/{id}/data") @GetMapping("/download/{id}/metadata")
@ResponseBody @ResponseBody
@Transactional @Transactional
public ResponseEntity<byte[]> data(@PathVariable String id) { public ResponseEntity<Map<String, Object>> metadata(@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();
return ResponseEntity.ok(Map.of(
// Check expiry "name", file.getName(),
if (file.getExpireyDateTime() != null && LocalDateTime.now().isAfter(file.getExpireyDateTime())) { "chunkCount", file.getChunkCount()));
storageService.delete(id);
fileRepository.delete(file);
return ResponseEntity.status(HttpStatus.GONE).build();
} }
// Check download limit before serving @GetMapping("/download/{id}/chunk/{index}")
if (file.getDownloadLimit() != null && file.getDownloads() >= file.getDownloadLimit()) { @ResponseBody
storageService.delete(id); @Transactional
fileRepository.delete(file); public ResponseEntity<byte[]> chunk(@PathVariable String id, @PathVariable Integer index) {
return ResponseEntity.status(HttpStatus.GONE).build(); AvailableFile available = getAvailableFile(id);
if (!available.found()) {
return ResponseEntity.status(available.status()).build();
} }
Optional<byte[]> data = storageService.get(id); File file = available.file();
if (index == null || index < 0 || index >= file.getChunkCount()) {
return ResponseEntity.badRequest().build();
}
Optional<byte[]> data = storageService.get(StorageKeys.chunk(file.getId(), index));
if (data.isEmpty()) { if (data.isEmpty()) {
return ResponseEntity.notFound().build(); return ResponseEntity.notFound().build();
} }
// Increment counter
file.setDownloads(file.getDownloads() + 1);
fileRepository.save(file);
// Clean up if limit now reached
if (file.getDownloadLimit() != null && file.getDownloads() >= file.getDownloadLimit()) {
storageService.delete(id);
fileRepository.delete(file);
}
String disposition = "attachment; filename=\""
+ file.getName().replace("\\", "\\\\").replace("\"", "\\\"") + "\"";
return ResponseEntity.ok() return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, disposition)
.contentType(MediaType.APPLICATION_OCTET_STREAM) .contentType(MediaType.APPLICATION_OCTET_STREAM)
.body(data.get()); .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();
}
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) {
for (int i = 0; i < file.getChunkCount(); i++) {
storageService.delete(StorageKeys.chunk(file.getId(), i));
}
}
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;
}
}
} }

View File

@@ -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,44 @@ 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(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) {
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.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}");
}
} }

View File

@@ -13,6 +13,7 @@ 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 LocalDateTime expireyDateTime; private LocalDateTime expireyDateTime;
private Integer downloadLimit; private Integer downloadLimit;
@Column(columnDefinition = "integer default 0") @Column(columnDefinition = "integer default 0")
@@ -41,6 +42,12 @@ 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 Integer getDownloadLimit() { public Integer getDownloadLimit() {
return downloadLimit; return downloadLimit;
} }

View File

@@ -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,15 @@ 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) {
for (int i = 0; i < file.getChunkCount(); i++) {
storageService.delete(StorageKeys.chunk(file.getId(), i));
}
}
} }

View File

@@ -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();

View File

@@ -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;
}
}

View File

@@ -1,26 +1,39 @@
async function encryptFile(arrayBuffer) { var DEFAULT_CHUNK_SIZE = 4 * 1024 * 1024;
async function encryptFile(file, chunkSize = DEFAULT_CHUNK_SIZE) {
const key = await crypto.subtle.generateKey( const key = await crypto.subtle.generateKey(
{ name: 'AES-GCM', length: 256 }, { name: 'AES-GCM', length: 256 },
true, true,
['encrypt', 'decrypt'] ['encrypt', 'decrypt']
); );
const rawKey = await crypto.subtle.exportKey('raw', key);
const hash = await hashKey(rawKey);
const base64urlKey = encodeKey(rawKey);
const chunkCount = Math.max(1, Math.ceil(file.size / chunkSize));
return {
hash,
base64urlKey,
chunkCount,
chunks: encryptedChunks(file, key, chunkCount, chunkSize)
};
}
async function* encryptedChunks(file, key, chunkCount, chunkSize) {
for (let index = 0; index < chunkCount; index++) {
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 iv = crypto.getRandomValues(new Uint8Array(12));
const ciphertext = await crypto.subtle.encrypt({ name: 'AES-GCM', iv }, key, plaintext);
const ciphertext = await crypto.subtle.encrypt({ name: 'AES-GCM', iv }, key, arrayBuffer);
// Payload: 12-byte IV prepended to ciphertext
const payload = new Uint8Array(12 + ciphertext.byteLength); const payload = new Uint8Array(12 + ciphertext.byteLength);
payload.set(iv, 0); payload.set(iv, 0);
payload.set(new Uint8Array(ciphertext), 12); payload.set(new Uint8Array(ciphertext), 12);
// SHA-256(rawKey) → file identifier sent to server; server never sees the key itself yield { index, payload };
const rawKey = await crypto.subtle.exportKey('raw', key); }
const hash = await hashKey(rawKey);
const base64urlKey = encodeKey(rawKey);
return { payload, hash, base64urlKey };
} }
async function hashKey(rawKey) { async function hashKey(rawKey) {
@@ -29,7 +42,7 @@ async function hashKey(rawKey) {
.join(''); .join('');
} }
async function decryptFile(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);

View File

@@ -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, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;'); return str.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
} }
@@ -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 });

View File

@@ -43,27 +43,44 @@ 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 encryptedFile = await encryptFile(selectedFile);
setStatus('Uploading\u2026'); for await (const { index, payload } of encryptedFile.chunks) {
const chunkCount = encryptedFile.chunkCount;
setProgress(`Encrypting chunk ${index + 1} of ${chunkCount}\u2026`, index, chunkCount);
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', encryptedFile.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', encryptedFile.chunkCount, encryptedFile.chunkCount);
const metadata = new FormData();
metadata.append('hash', encryptedFile.hash);
metadata.append('name', selectedFile.name);
metadata.append('chunkCount', encryptedFile.chunkCount);
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' });
htmx.process(dropZone); htmx.process(dropZone);
htmx.find('#share-link').value = window.location.origin + '/download#' + base64urlKey; htmx.find('#share-link').value = window.location.origin + '/download#' + encryptedFile.base64urlKey;
} catch (err) { } catch (err) {
htmx.swap(dropZone, ` htmx.swap(dropZone, `
@@ -79,6 +96,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 = '';

View File

@@ -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">&#x1F512;</div> <div class="drop-zone-icon mb-3">&#x1F512;</div>
<div class="drop-zone-text" id="download-status">Preparing&hellip;</div> <div class="drop-zone-text mb-3" id="download-status">Preparing&hellip;</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>