Skip to content

Commit 2df37e9

Browse files
committed
fix: relax chunk limits, support proxies, optimize reads
Signed-off-by: skidoodle <contact@albert.lol>
1 parent 722dbaa commit 2df37e9

File tree

4 files changed

+22
-8
lines changed

4 files changed

+22
-8
lines changed

internal/app/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const (
2222
ShutdownTimeout = 10 * time.Second
2323

2424
UploadChunkSize = 8 << 20
25+
MinChunkSize = 1 << 20
2526
MaxRequestOverhead = 10 << 20
2627
PermUserRWX = 0o700
2728
MegaByte = 1 << 20

internal/app/server.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,12 @@ func (app *App) RespondWithLink(writer http.ResponseWriter, request *http.Reques
7272
return
7373
}
7474

75-
scheme := "https"
76-
if request.TLS == nil {
77-
scheme = "http"
75+
scheme := request.Header.Get("X-Forwarded-Proto")
76+
if scheme == "" {
77+
scheme = "https"
78+
if request.TLS == nil {
79+
scheme = "http"
80+
}
7881
}
7982

8083
if _, err := fmt.Fprintf(writer, "%s://%s\n", scheme, link); err != nil {

internal/app/upload.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func (app *App) HandleChunk(writer http.ResponseWriter, request *http.Request) {
112112
return
113113
}
114114

115-
maxChunks := int((app.Conf.MaxMB*MegaByte)/UploadChunkSize) + ChunkSafetyMargin
115+
maxChunks := int((app.Conf.MaxMB*MegaByte)/MinChunkSize) + ChunkSafetyMargin
116116

117117
if !reUploadID.MatchString(uid) || idx > maxChunks || idx < 0 {
118118
app.SendError(writer, request, http.StatusBadRequest)
@@ -148,7 +148,7 @@ func (app *App) HandleFinish(writer http.ResponseWriter, request *http.Request)
148148
return
149149
}
150150

151-
maxChunks := int((app.Conf.MaxMB*MegaByte)/UploadChunkSize) + ChunkSafetyMargin
151+
maxChunks := int((app.Conf.MaxMB*MegaByte)/MinChunkSize) + ChunkSafetyMargin
152152

153153
if !reUploadID.MatchString(uid) || total > maxChunks || total <= 0 {
154154
app.SendError(writer, request, http.StatusBadRequest)

internal/crypto/reader.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type Decryptor struct {
1616
aead cipher.AEAD
1717
size int64
1818
offset int64
19+
phyOffset int64
1920
}
2021

2122
func NewDecryptor(readSeeker io.ReadSeeker, aead cipher.AEAD, encryptedSize int64) *Decryptor {
@@ -35,6 +36,7 @@ func NewDecryptor(readSeeker io.ReadSeeker, aead cipher.AEAD, encryptedSize int6
3536
aead: aead,
3637
size: plainSize,
3738
offset: 0,
39+
phyOffset: -1,
3840
}
3941
}
4042

@@ -49,14 +51,22 @@ func (d *Decryptor) Read(buf []byte) (int, error) {
4951
overhead := int64(d.aead.Overhead())
5052
actualChunkSize := int64(GCMChunkSize) + overhead
5153

52-
_, err := d.readSeeker.Seek(chunkIdx*actualChunkSize, io.SeekStart)
53-
if err != nil {
54-
return 0, fmt.Errorf("failed to seek: %w", err)
54+
targetOffset := chunkIdx * actualChunkSize
55+
56+
if d.phyOffset != targetOffset {
57+
if _, err := d.readSeeker.Seek(targetOffset, io.SeekStart); err != nil {
58+
return 0, fmt.Errorf("failed to seek: %w", err)
59+
}
60+
d.phyOffset = targetOffset
5561
}
5662

5763
encrypted := make([]byte, actualChunkSize)
5864

5965
bytesRead, err := io.ReadFull(d.readSeeker, encrypted)
66+
if bytesRead > 0 {
67+
d.phyOffset += int64(bytesRead)
68+
}
69+
6070
if err != nil && !errors.Is(err, io.ErrUnexpectedEOF) {
6171
return 0, fmt.Errorf("failed to read encrypted data: %w", err)
6272
}

0 commit comments

Comments
 (0)