Exemplo n.º 1
0
        public async Task <IActionResult> AnalyzeClip([FromForm] IFormFile file,
                                                      [FromForm] string projectId,
                                                      [FromForm] string clipId,
                                                      [FromForm] string userId,
                                                      [FromForm] string footagePath)
        {
            // clipId, projectId, and userId are required parameters
            if (clipId is null || projectId is null || userId is null)
            {
                StatusCode(StatusCodes.Status400BadRequest,
                           new
                {
                    message = $"clipId, projectId, and userId must all not be null!\n" +
                              $"(userId:{userId}//project:{projectId}//clip:{clipId})"
                });
            }

            // read from DB, get response if it exists
            var query =
                from p in _montageContext.AdobeProjects
                where (p.ProjectId.Equals(projectId))
                join a in _montageContext.ClipAssignments
                on p.ProjectId equals a.ProjectId

                join c in _montageContext.AdobeClips
                .Where(c => c.ClipId.Equals(clipId))
                .DefaultIfEmpty()
                on a.ClipId equals c.ClipId

                select c;

            // IF clip+project seen, collect from db
            if (query.Any())
            {
                AdobeClip      clip   = query.FirstOrDefault();
                AnalysisResult result = (AnalysisResult.DeserializeResponse(clip.AnalysisResultString));

                // if the result in the db has an error, delete it and try again
                if (result is null || result.Error)
                {
                    // delete result from db
                    try
                    {
                        _montageContext.AdobeClips.Remove(clip);
                        _montageContext.SaveChanges();
                    }
                    catch (Exception e)
                    {
                        return(StatusCode(StatusCodes.Status500InternalServerError,
                                          new
                        {
                            message = $"(this shouldn't happen!) Unable to repair malformed clip (userId:{userId}//project:{projectId}//clip:{clipId})\n" +
                                      "Please try adding under another clipId\n" +
                                      $"Exception:\n{e}"
                        }));
                    }

                    // re-add it
                    return(await ProcessNewClip(file, projectId, clipId, userId, footagePath));
                }
                else
                {
                    // return read from db
                    // footagePath with update if provided
                    result.FootagePath = footagePath ?? result.FootagePath;

                    clip.AnalysisResultString = result.Serialize();

                    try
                    {
                        _montageContext.Update(clip);
                        await _montageContext.SaveChangesAsync();
                    }
                    catch (Exception e)
                    {
                        // this REALLY should never happen
                        return(StatusCode(StatusCodes.Status500InternalServerError,
                                          new
                        {
                            message = $"Unable to update clip with new footage path" +
                                      $"Exception:\n{e}"
                        }));
                    }

                    return(Ok(result));
                }
            }