File upload

mail@pastecode.io avatar
unknown
csharp
7 months ago
3.7 kB
1
Indexable
Never
[Consumes("multipart/form-data")]
    [HttpPost("{groupId}")]
    [DisableRequestSizeLimit,
     RequestFormLimits(MultipartBodyLengthLimit = int.MaxValue, 
         ValueLengthLimit = int.MaxValue)]
    public async Task<IActionResult> UploadFiles(string groupId,IEnumerable<IFormFile>? files)
    {
        int userId = _apiAuthenticationService.GetAuthenticatedUserId();
        var group = await _context.CaseGroups
            .Include(x => x.ApplicationUserCaseGroups)
            .FirstOrDefaultAsync(x => x.Id == groupId);

        if (files != null)
        {
            if (group != null && group.ApplicationUserCaseGroups.Any(x => x.ApplicationUserId == userId))
            {
                foreach (IFormFile file in files)
                {
                    try
                    {
                        var fileContent = ContentDispositionHeaderValue.Parse(file.ContentDisposition);
                        var fileName = Path.GetFileName(fileContent.FileName.ToString().Trim('"'));
                        var extension = Path.GetExtension(fileName);
                        
                        var newaudioRecordTmp = new AudioRecord
                        {
                            Filename = fileName,
                            CaseGroupId = groupId,
                            CreatedAt = DateTime.UtcNow,
                            ContentDescription = ""
                        };
                        await _context.AudioRecords.AddAsync(newaudioRecordTmp);
                        await _context.SaveChangesAsync();
                        /*await using (var stream =
                                     new FileStream(
                                         _storageHelper.GetPhonexiaFullFilename(newaudioRecordTmp.Id, extension),
                                         FileMode.Create))
                        {*/

                            //await file.CopyToAsync(stream);
                            await UploadFileToPhonexiaServer(file.OpenReadStream(), newaudioRecordTmp);
                            newaudioRecordTmp.UploadedAt = DateTime.UtcNow;
                            await _context.SaveChangesAsync();
                        /*}*/
                        

                        if (group.AsrAutoTranstriptionModelsInThisCase != null)
                            foreach (string s in group.AsrAutoTranstriptionModelsInThisCase)
                            {

                                SpeechToTextRequest newRequest = new SpeechToTextRequest
                                {
                                    AudioRecordId = newaudioRecordTmp.Id,
                                    Model = s,
                                    ResultType = SpeechToTextController._confusionNetworkString,
                                    CreatedAt = DateTime.UtcNow
                                };
                                await _context.SpeechToTextRequests.AddAsync(newRequest);
                            }

                        await _context.SaveChangesAsync();
                    }
                    catch (Exception exception)
                    {
                        Log.Logger.Error(exception, $"FilesController - Cant save file to Phonexia Folder");
                        return Problem(exception.Message);
                    }
                    await Task.Yield();
                }
            }
            else
            {
                return Unauthorized();
            }
        }
        else
        {
            return NoContent();
        }
        return Ok();
    }
Leave a Comment