public async Task <IActionResult> Upload(IFormFile file)
        {
            if (file.Length > 0)
            {
                using (var memoryStream = new MemoryStream())
                {
                    file.CopyTo(memoryStream);
                    var bytes = memoryStream.ToArray();
                    await _fileProcessingService.ProcessFile(bytes);
                }
                return(Ok());
            }

            return(BadRequest());
        }
Example #2
0
        public async Task DoWork()
        {
            var directories = _fileShareQuery.GetDirectories(FILESHARE_LOCAL_ROOT);

            foreach (string companyDirectory in directories)
            {
                var fileNames = await _fileShareQuery.GetFileNamesForNetworkLocationAsync(companyDirectory);

                _logger.LogInformation($"Retrieved {fileNames.Count()} files from network path: {companyDirectory}");

                foreach (string fileNameWithPath in fileNames)
                {
                    var fileName = Path.GetFileName(fileNameWithPath);
                    if (fileName.IsValidFileName())
                    {
                        string documentId   = fileName.GetDocumentId();
                        string documentName = fileName.GetDocumentName();
                        var    keywords     = await _fileProcessingService.ProcessFile(fileNameWithPath);

                        if (keywords != null)
                        {
                            await _lookupStore.Record(documentId, documentName, keywords);

                            //future improvement - batch success files and delete after every file has been processed
                            await _fileDeletionService.DeleteFileAsync(fileNameWithPath);
                        }
                        else
                        {
                            _logger.LogWarning($"File processed unsuccesfully: {fileName}, result will not be stored");
                            //future improvement: add retry mechanism for unsuccessful files
                        }
                    }
                    else
                    {
                        _logger.LogWarning($"Invalid filename: {fileName}");
                    }
                }
            }
        }