Skip to content

Commit a009ba7

Browse files
committed
handle conflict in postFile
Signed-off-by: Callum Styan <callumstyan@gmail.com>
1 parent d7b1253 commit a009ba7

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

coderd/files.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,22 @@ func (api *API) postFile(rw http.ResponseWriter, r *http.Request) {
118118
Data: data,
119119
})
120120
if err != nil {
121-
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
122-
Message: "Internal error saving file.",
123-
Detail: err.Error(),
124-
})
125-
return
121+
if database.IsUniqueViolation(err, database.UniqueFilesHashCreatedByKey) {
122+
// The file was uploaded by some concurrent process since the last time we checked for it, fetch it again.
123+
file, err = api.Database.GetFileByHashAndCreator(ctx, database.GetFileByHashAndCreatorParams{
124+
Hash: hash,
125+
CreatedBy: apiKey.UserID,
126+
})
127+
}
128+
// At this point the first error was either not the UniqueViolation OR there's still an error even after we
129+
// attempt to fetch the file again, so we should return here.
130+
if err != nil {
131+
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
132+
Message: "Internal error saving file.",
133+
Detail: err.Error(),
134+
})
135+
return
136+
}
126137
}
127138

128139
httpapi.Write(ctx, rw, http.StatusCreated, codersdk.UploadResponse{

0 commit comments

Comments
 (0)