File tree Expand file tree Collapse file tree 1 file changed +16
-5
lines changed Expand file tree Collapse file tree 1 file changed +16
-5
lines changed Original file line number Diff line number Diff line change @@ -118,11 +118,22 @@ func (api *API) postFile(rw http.ResponseWriter, r *http.Request) {
118
118
Data : data ,
119
119
})
120
120
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
+ }
126
137
}
127
138
128
139
httpapi .Write (ctx , rw , http .StatusCreated , codersdk.UploadResponse {
You can’t perform that action at this time.
0 commit comments