diff --git a/internal/storage/FileServing.go b/internal/storage/FileServing.go index 64ce9f70..8d5516ac 100644 --- a/internal/storage/FileServing.go +++ b/internal/storage/FileServing.go @@ -647,8 +647,10 @@ func ServeFile(file models.File, w http.ResponseWriter, r *http.Request, forceDo // confirm that the file has been completely downloaded. It expires automatically after 24 hours. statusId := downloadstatus.SetDownload(file) isBlocking, err := aws.ServeFile(w, r, file, forceDownload, forceDecryption) - // TODO chances are high that an error is returned here, we should consider proper output - helper.Check(err) + if err != nil { + fmt.Println(err) + _, _ = w.Write([]byte("Error serving file")) + } if isBlocking { downloadstatus.SetComplete(statusId) } diff --git a/internal/storage/FileServing_test.go b/internal/storage/FileServing_test.go index d8369d62..e5bc8129 100644 --- a/internal/storage/FileServing_test.go +++ b/internal/storage/FileServing_test.go @@ -625,6 +625,40 @@ func TestServeFile(t *testing.T) { test.ResponseBodyContains(t, w, "Error decrypting file") } +func TestServeFileAwsErrorHandling(t *testing.T) { + if !aws.IsIncludedInBuild { + t.Skip("AWS support not included in build") + } + testconfiguration.EnableS3() + config, ok := cloudconfig.Load() + test.IsEqualBool(t, ok, true) + ok = aws.Init(config.Aws) + test.IsEqualBool(t, ok, true) + + // A file with an AWS bucket set, but never actually uploaded, causes aws.ServeFile to + // return an error. ServeFile must handle that gracefully instead of panicking. + // forceDecryption is set to true so aws.ServeFile takes the serveDecryptedFile path, + // which calls s3.GetObject directly instead of just presigning a redirect URL - only + // that path actually contacts S3 and surfaces the missing-object error. + file := models.File{ + Id: "awsErrorHandlingTest1", + Name: "aws error handling test", + AwsBucket: "gokapi-test", + SHA1: "nonexistentAwsObjectSha1", + ExpireAt: time.Now().Add(time.Hour).Unix(), + SizeBytes: 10, + } + database.SaveMetaData(file) + + r := httptest.NewRequest("GET", "/", nil) + w := httptest.NewRecorder() + ServeFile(file, w, r, false, true, true, false) + test.ResponseBodyContains(t, w, "Error serving file") + + database.DeleteMetaData(file.Id) + testconfiguration.DisableS3() +} + func TestCleanUp(t *testing.T) { files := database.GetAllMetadata() downloadstatus.DeleteAll()