Gitlab Community Edition Instance

Commit 57962db0 authored by mhellka's avatar mhellka
Browse files

tus: Expire uploads 24h after creation.

Previously, they were expired 24h after the last HEAD. If HEAD was never called, or called every 24h, the upload would never expire.
parent 834f56b0
......@@ -3,9 +3,9 @@ package de.gwdg.cdstar.rest.ext.tus;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.file.StandardOpenOption;
import java.time.Duration;
import java.time.Instant;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import de.gwdg.cdstar.Promise;
import de.gwdg.cdstar.Utils;
......@@ -32,6 +32,8 @@ public class TusBlueprint implements RestBlueprint {
private static final String TUS_VERSION = "1.0.0";
private final TusService chunkService;
private Duration defaultExpire = Duration.ofHours(24);
public TusBlueprint(TusService service) {
chunkService = service;
}
......@@ -39,13 +41,13 @@ public class TusBlueprint implements RestBlueprint {
@Override
public void configure(RestConfig cfg) {
cfg.route("/")
.POST(this::handleCreate)
.target("OPTIONS", this::handleOptions);
.POST(this::handleCreate)
.target("OPTIONS", this::handleOptions);
cfg.route("/<chunk:re:[^/]+>")
.PATCH(this::handlePatch)
.DELETE(this::handleDelete)
.HEAD(this::handleHead);
.PATCH(this::handlePatch)
.DELETE(this::handleDelete)
.HEAD(this::handleHead);
}
public Void handleCreate(RestContext ctx) {
......@@ -60,6 +62,7 @@ public class TusBlueprint implements RestBlueprint {
final TusFile chunk = chunkService.createChunk();
chunk.setLength(length);
chunk.setMeta(meta);
chunk.expireIn(defaultExpire);
ctx.status(201);
ctx.header(TUS_RESUMABLE, TUS_VERSION);
......@@ -85,8 +88,6 @@ public class TusBlueprint implements RestBlueprint {
checkTusHeader(ctx);
final TusFile chunk = getChunk(ctx.getPathParam("chunk"));
chunk.expireIn(24, TimeUnit.HOURS);
ctx.status(200);
ctx.header("Cache-Control", "no-store");
ctx.header(TUS_RESUMABLE, TUS_VERSION);
......@@ -151,20 +152,20 @@ public class TusBlueprint implements RestBlueprint {
try {
final FileChannel ch = FileChannel.open(chunk.getPath(), StandardOpenOption.CREATE,
StandardOpenOption.WRITE, StandardOpenOption.APPEND);
StandardOpenOption.WRITE, StandardOpenOption.APPEND);
Promise.wrap(new AsyncUpload(ctx, ch).dispatch())
.then(r -> {
Utils.closeQuietly(ch);
chunk.unlock();
ctx.header(TUS_UPLOAD_OFFSET, chunk.getOffset());
ctx.close();
}, err -> {
Utils.closeQuietly(ch);
chunk.unlock();
ctx.header(TUS_UPLOAD_OFFSET, chunk.getOffset());
ctx.abort(err);
});
.then(r -> {
Utils.closeQuietly(ch);
chunk.unlock();
ctx.header(TUS_UPLOAD_OFFSET, chunk.getOffset());
ctx.close();
}, err -> {
Utils.closeQuietly(ch);
chunk.unlock();
ctx.header(TUS_UPLOAD_OFFSET, chunk.getOffset());
ctx.abort(err);
});
return null;
} catch (final IOException e) {
......@@ -176,19 +177,19 @@ public class TusBlueprint implements RestBlueprint {
private TusFile getChunk(String id) {
return chunkService
.getChunk(id)
.orElseThrow(() -> new ErrorResponse(404, "TusError", "Chunk not found or expired"));
.getChunk(id)
.orElseThrow(() -> new ErrorResponse(404, "TusError", "Chunk not found or expired"));
}
private void checkTusHeader(RestContext ctx) {
if (!Utils.equal(ctx.getHeader(TUS_RESUMABLE), TUS_VERSION))
throw new ErrorResponse(400, "TusError", "Tus-Resumable header missing or wrong version.")
.detail("expected", TUS_VERSION);
.detail("expected", TUS_VERSION);
}
private long getUploadLength(RestContext ctx) {
String lengthHeader = ctx.getHeader(TUS_UPLOAD_LENGTH);
if(lengthHeader == null)
if (lengthHeader == null)
return -1;
try {
......
......@@ -3,8 +3,8 @@ package de.gwdg.cdstar.rest.ext.tus;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.time.Instant;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -70,8 +70,8 @@ class TusFile {
}
}
public void expireIn(long duration, TimeUnit unit) {
expire = Instant.now().plusMillis(unit.toMillis(duration)).toEpochMilli();
public void expireIn(Duration timeout) {
expire = Instant.now().plus(timeout).toEpochMilli();
}
public void expire() {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment