Example #1
0
        public async Task <ActionResult> PostConvert(int id, [FromBody] ConvertArg arg, [FromServices] ConvertService cs)
        {
            var user = await GetLoginUser();

            if (user == null)
            {
                return(GetErrorResult("no_login"));
            }

            var track = await _context.GetTrack(id);

            if (track?.IsVisibleToUser(user) != true)
            {
                return(GetErrorResult("track_not_found"));
            }

            var profile = arg.profile;

            if (profile == null)
            {
                return(GetErrorResult("no_profile"));
            }

            if (track.files == null)
            {
                track.files = new List <TrackFile>();
            }
            var file = track.files.Find(x => x.ConvName == profile);

            if (file == null)
            {
                var convObj = _context.MCloudConfig.FindConverter(profile);
                if (convObj == null)
                {
                    return(GetErrorResult("profile_not_found"));
                }

                var r = await cs.GetConverted(_context, track, convObj);

                file = r.TrackFile;
                if (!r.AlreadyExisted)
                {
RETRY:
                    _context.Files.Add(file.File);
                    _context.TrackFiles.Add(file);
                    track.files.Add(file);
                    if (await _context.FailedSavingChanges())
                    {
                        goto RETRY;
                    }
                }
            }

            return(new JsonResult(new TrackFileVM(file)));
        }