public async Task InsertFileAsync(Files.Models.File file) { if (file == default) { throw new NullReferenceException($"Parameter {nameof(file)} passed into {nameof(InsertFileAsync)} is null."); } await databaseContext.AddAsync(file); }
public void DeleteFile(Files.Models.File file) { if (file == default) { throw new NullReferenceException($"Parameter {nameof(file)} passed into {nameof(DeleteFile)} is null."); } databaseContext.Files.Remove(file); }
public async Task <Files.Models.File> GetFileAsync(string fileExternalId) { Files.Models.File file = await databaseContext.Files.FirstOrDefaultAsync(t => t.ExternalId == fileExternalId); if (file == default) { throw new NullReferenceException($"{nameof(Files.Models.File)} with {nameof(fileExternalId)} = {fileExternalId} not exist."); } return(file); }
public async Task <IActionResult> AddFile(IFormFile uploadedFile, string lD, string sD) { if (uploadedFile != null) { // путь к папке Files string path = "/Files/" + uploadedFile.Name; // сохраняем файл в папку Files в каталоге wwwroot using (var fileStream = new FileStream(_appEnvironment.WebRootPath + path, FileMode.Create)) { await uploadedFile.CopyToAsync(fileStream); } File file = new File { Name = uploadedFile.FileName, Path = path, LongDescription = lD, ShortDescription = sD }; _context.Files.Add(file); _context.SaveChanges(); } return(RedirectToAction("Index")); }
public async Task DeleteFileAsync(string fileExternalId) { Files.Models.File file = await GetFileAsync(fileExternalId); DeleteFile(file); }
public async Task DeleteFileAsync(int fileId) { Files.Models.File file = await GetFileAsync(fileId); DeleteFile(file); }
public bool IsFileDefault(Files.Models.File file) => databaseContext.IsFileDefault(file);
private bool UserHaveAccessToFile(string userExternalId, Files.Models.File file) => file.UserExternalId.Equals(userExternalId);
public async Task <ActionResult <FileDto> > UploadFile(string userid) { if (!ExternalIdPassedGuidValidation(userid)) { return(StatusCode(StatusCodes.Status400BadRequest, $"Invalid {nameof(userid)}.")); } try { if (!await UserExists(userid)) { return(StatusCode(StatusCodes.Status400BadRequest, "Unknown user external id.")); } } catch (Exception ex) { return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message)); } IFormFile file; try { file = Request.Form.Files[0]; if (file == default) { throw new NullReferenceException("File in request body does not exists."); } } catch (Exception ex) { return(StatusCode(StatusCodes.Status400BadRequest, ex.Message)); } string filepath; try { System.IO.DirectoryInfo dirInfo = new System.IO.DirectoryInfo($"{appEnvironment.WebRootPath}/Storages/{userid}"); if (!dirInfo.Exists) { dirInfo.Create(); } filepath = $"/Storages/{userid}/{file.FileName}"; using (System.IO.FileStream fileStream = new System.IO.FileStream(appEnvironment.WebRootPath + filepath, System.IO.FileMode.Create)) await file.CopyToAsync(fileStream); } catch (Exception ex) { return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message)); } Files.Models.File newFile; try { newFile = new Files.Models.File(filepath) { UserExternalId = userid }; await databaseRepository.InsertFileAsync(newFile); await databaseRepository.SaveChangesAsync(); } catch (Exception ex) { return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message)); } FileDto fileDto; try { fileDto = mapper.Map <FileDto>(newFile); } catch (Exception ex) { return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message)); } return(Ok(fileDto)); }