FIX: Gap Of Uploading The Same File Key Twice
Build, Push and Deploy CMS / build-push-deploy (push) Successful in 2m19s
Build, Push and Deploy CMS / build-push-deploy (push) Successful in 2m19s
This commit is contained in:
@@ -113,8 +113,9 @@ func PresignVideoUpload(w http.ResponseWriter, r *http.Request) {
|
||||
// @Success 201 {object} api.CompleteResponse
|
||||
// @Failure 400 {object} api.ProblemDetails "Request body was not valid JSON"
|
||||
// @Failure 404 {object} api.ProblemDetails "categoryIds referenced a category that does not exist"
|
||||
// @Failure 409 {object} api.ProblemDetails "key has already been registered by an earlier request"
|
||||
// @Failure 422 {object} api.ProblemDetails "title was empty, key was missing or not a key issued by this API, or categoryIds was empty"
|
||||
// @Failure 500 {object} api.ProblemDetails "Categories could not be read, the transcoding job could not be queued, or the video record could not be saved"
|
||||
// @Failure 500 {object} api.ProblemDetails "Categories could not be read, the key could not be checked, the transcoding job could not be queued, or the video record could not be saved"
|
||||
// @Router /api/videos [post]
|
||||
func CompleteVideoUpload(w http.ResponseWriter, r *http.Request) {
|
||||
r.Body = http.MaxBytesReader(w, r.Body, maxJSONBodySize)
|
||||
@@ -166,6 +167,24 @@ func CompleteVideoUpload(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
// Ahead of queueing, because a key that is already registered would
|
||||
// otherwise get a second transcoding job before the insert failed on the
|
||||
// storage_key UNIQUE constraint. That constraint is still the backstop:
|
||||
// this check and the insert are not one atomic step, so two concurrent
|
||||
// requests carrying the same key can both get past here.
|
||||
alreadyRegistered, err := repositories.VideoRepo.VideoExistsWithStorageKey(r.Context(), key)
|
||||
if err != nil {
|
||||
log.Printf("Something Went Wrong Checking Whether The Video Was Already Registered: %s", err)
|
||||
writeProblem(w, http.StatusInternalServerError, "Something Went Wrong Checking Whether The Video Was Already Registered",
|
||||
"The submitted key could not be checked against the existing video records, so no video record was created and no transcoding job was queued. This is a server-side fault; the uploaded file is still in storage, so retry this request with the same 'key'.")
|
||||
return
|
||||
}
|
||||
if alreadyRegistered {
|
||||
writeProblem(w, http.StatusConflict, "Video Already Registered",
|
||||
fmt.Sprintf("The 'key' %q has already been registered, so a video record and a transcoding job exist for that file. Registering it a second time would queue a duplicate job. If you meant to add a different video, request a fresh key from POST /api/videos/presign and upload the file to it.", key))
|
||||
return
|
||||
}
|
||||
|
||||
jobID, err := services.MediaConvertClient.QueueEncodingJob(r.Context(), key)
|
||||
if err != nil {
|
||||
log.Printf("Something Went Wrong On Creation Of Transcoding Job: %s", err)
|
||||
|
||||
Reference in New Issue
Block a user