/// <inheritdoc/> public async Task <TempFileLog> UploadFileAsync(IFormFile formFile) { try { string saveDirectory = this.hostingEnvironment.GetTempUploadDirectory(); string resultFileName = FilesFunctions.GetUniqueFileName(); string resultFileExtension = formFile.FileName.Split('.').LastOrDefault(); string relativeSaveDirectory = Path.Combine(Folders.PrivateRootFolderName, Folders.UploadFolderName, Folders.TempFolderName); string fileFullPath = Path.Combine(saveDirectory, $"{resultFileName}.{resultFileExtension}"); string fileRelativePath = Path.Combine(relativeSaveDirectory, $"{resultFileName}.{resultFileExtension}"); using (FileStream stream = System.IO.File.Create(fileFullPath)) { formFile.CopyTo(stream); stream.Flush(); } TempFileLog fileEntity = new TempFileLog { Name = resultFileName, Path = fileRelativePath, FileExtension = FilesFunctions.GetFileExtension(resultFileExtension), }; fileEntity.FileType = FilesFunctions.GetFileType(fileEntity.FileExtension); this.loggerContext.TempFileLogs.Add(fileEntity); await this.loggerContext.SaveChangesAsync(); return(fileEntity); } catch (Exception ex) { await this.logger.LogErrorAsync(ex); return(default);
/// <inheritdoc/> public async Task <string> CreateAvatarFromUrlAsync(string externalSourceAvatarUrl) { try { string targetFileDirectoryPath = this.rootsService.GetPathFromPublicRoot(Folders.UploadFolderName, Folders.ImagesFolderName); string avatarName = FilesFunctions.GetUniqueFileName(); string avatarNameWithExtension = $"{avatarName}.jpg"; string targetFilePath = Path.Combine(targetFileDirectoryPath, avatarNameWithExtension); using (var client = new HttpClient()) using (var response = await client.GetAsync(externalSourceAvatarUrl)) using (Stream stream = await response.Content.ReadAsStreamAsync()) using (FileStream fileStream = System.IO.File.Create(targetFilePath)) { if (response.IsSuccessStatusCode) { stream.CopyTo(fileStream); return($"/{Folders.UploadFolderName}/{Folders.ImagesFolderName}/{avatarNameWithExtension}"); } } return(null); } catch (Exception ex) { await this.logger.LogErrorAsync(ex); return(null); } }
/// <inheritdoc/> public async Task <string> CreateUserAvatarAsync(byte[] avatarFileByteArray) { try { string avatarRelativePath = Path.Combine(Folders.UploadFolderName, Folders.ImagesFolderName); string targetFileDirectoryPath = Path.Combine(this.hostEnvironment.ContentRootPath, Folders.PublicRootFolderName, avatarRelativePath); string avatarName = FilesFunctions.GetUniqueFileName(); string avatarNameWithExtension = $"{avatarName}.jpg"; string targetFilePath = Path.Combine(targetFileDirectoryPath, avatarNameWithExtension); using (Stream stream = new MemoryStream(avatarFileByteArray)) using (FileStream fileStream = System.IO.File.Create(targetFilePath)) { stream.CopyTo(fileStream); return($"/{Folders.UploadFolderName}/{Folders.ImagesFolderName}/{avatarNameWithExtension}"); } } catch (Exception ex) { await this.logger.LogErrorAsync(ex); return(null); } }
public async Task <FileDto> UploadFileAsync(IFormFile formFile) { try { string saveDirectory = this.hostingEnvironment.GetPrivateRootTempUploadDirectory(); string resultFileName = FilesFunctions.GetUniqueFileName(); string resultFileExtension = formFile.FileName.Split('.').LastOrDefault(); string relativeSaveDirectory = Path.Combine(Constants.PrivateRootFolderName, Constants.UploadFolderName, Constants.TempFolderName); string fileFullPath = Path.Combine(saveDirectory, $"{resultFileName}.{resultFileExtension}"); string fileRelativePath = Path.Combine(relativeSaveDirectory, $"{resultFileName}.{resultFileExtension}"); using (FileStream stream = System.IO.File.Create(fileFullPath)) { formFile.CopyTo(stream); stream.Flush(); } Mvc567.Entities.Database.File fileEntity = new Mvc567.Entities.Database.File { Name = resultFileName, Path = fileRelativePath, Temp = true, FileExtension = FilesFunctions.GetFileExtension(resultFileExtension) }; fileEntity.FileType = FilesFunctions.GetFileType(fileEntity.FileExtension); this.uow.GetStandardRepository().Add <Mvc567.Entities.Database.File>(fileEntity); await this.uow.SaveChangesAsync(); var fileDto = this.mapper.Map <FileDto>(fileEntity); return(fileDto); } catch (Exception ex) { await this.LogErrorAsync(ex, nameof(UploadFileAsync)); return(null); } }