Exemple #1
0
 private static void EnsureArgumentsAreValid(Stream stream, FileInfo fileInfo)
 {
     if (stream == null)
     {
         throw new ArgumentNullException(nameof(stream));
     }
     if (fileInfo == null)
     {
         throw new ArgumentNullException(nameof(fileInfo));
     }
     if (!fileInfo.MimeType.IsNullOrWhiteSpace())
     {
         return;
     }
     if (fileInfo.Filename.IsNullOrWhiteSpace() && fileInfo.Extension.IsNullOrWhiteSpace())
     {
         throw new ArgumentException("Filename and extension are null.");
     }
     if (!fileInfo.Extension.IsNullOrWhiteSpace())
     {
         return;
     }
     fileInfo.Extension = Path.GetExtension(fileInfo.Filename);
     if (fileInfo.Extension.IsNullOrWhiteSpace())
     {
         throw new ArgumentException("After parsing from filename extension is still empty.");
     }
 }
Exemple #2
0
        public async Task <File> UploadFileAsync(string base64String, FileInfo fileInfo, bool needToSaveChanges = true)
        {
            var stream   = GetStreamFromBase64File(base64String);
            var mimeType = GetMimeTypeFromBase64File(base64String);

            fileInfo ??= new FileInfo();
            fileInfo.MimeType = mimeType;
            return(await UploadFileAsync(stream, fileInfo, needToSaveChanges));
        }
Exemple #3
0
        public async Task <File> UploadFileAsync(IFormFile file, bool needToSaveChanges = true)
        {
            var stream   = file.OpenReadStream();
            var fileInfo = new FileInfo
            {
                Filename = file.FileName,
                MimeType = file.ContentType
            };

            return(await UploadFileAsync(stream, fileInfo, needToSaveChanges));
        }
Exemple #4
0
        public async Task <File> UploadFileAsync(Stream stream, FileInfo fileInfo, bool needToSaveChanges = true)
        {
            EnsureArgumentsAreValid(stream, fileInfo);
            await using var mnpContext = _contextFactory.Create();

            var fileHash = fileInfo.Hash;

            if (fileHash.IsNullOrWhiteSpace())
            {
                fileHash = ComputeMd5Hash(stream);
            }
            var fileInDb = await mnpContext.Files.AsNoTracking().SingleOrDefaultAsync(f => f.Hash == fileHash);

            if (fileInDb != null)
            {
                _logger.LogInformation($"File with hash {fileHash} already uploaded.");
                return(fileInDb);
            }

            var mimeType   = fileInfo.MimeType ?? MimeTypes.GetMimeType(fileInfo.Extension);
            var filePath   = BuildFilePath(fileHash);
            var fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write);
            await stream.CopyToAsync(fileStream);

            await fileStream.DisposeAsync();

            var file = new File
            {
                CreationDate = DateTime.Now,
                FileLink     = "/Files/" + fileHash,
                Filename     = fileInfo.Filename ?? fileHash,
                Hash         = fileHash,
                MimeType     = mimeType
            };

            if (needToSaveChanges)
            {
                await mnpContext.AddAsync(file);

                await mnpContext.SaveChangesAsync();
            }

            return(file);
        }