/// <summary> /// This method opens a StreamReader or WriteReader. /// </summary> /// <param name="type">Read or Write or Nothing.</param> /// <returns> /// if the connection was made with Read or Write type - true. /// </returns> /// <inheritdoc /> public bool Open(FileManagerType type) { if (!_fileSystem.File.Exists(_path)) { return(false); } _type = type; switch (_type) { case FileManagerType.Read: _sr = new StreamReader(_fileSystem.File.Open(_path, FileMode.Open)); break; case FileManagerType.Write: _sw = new StreamWriter(_fileSystem.File.Open(_path, FileMode.Open)); break; case FileManagerType.Nothing: return(false); } return(true); }
public bool DeleteImage(string imageName, FileManagerType.FileType typeId) { var type = FileManagerType.ParseType(typeId); var image = Path.Combine(_webRootPath + "/" + type, imageName); if (!File.Exists(image)) { return(false); } File.Delete(image); return(true); }
/// <summary> /// upload image with type /// </summary> /// <param name="image"></param> /// <param name="typeId"> type of the image that have to upload </param> /// <returns> name of the image (without path :D) </returns> public async Task <string> UploadImage(IFormFile image, FileManagerType.FileType typeId) { var type = FileManagerType.ParseType(typeId); var uploadPath = Path.Combine(_webRootPath + "/" + type); if (!Directory.Exists(uploadPath)) { Directory.CreateDirectory(uploadPath); } if (image.ContentType == "image/jpeg" || image.ContentType == "image/svg+xml" || image.ContentType == "image/png" || image.ContentType == "image/jpg") { var filePath = Path.Combine(uploadPath, image.FileName); await using var fileStream = new FileStream(filePath, FileMode.Create); await image.CopyToAsync(fileStream).ConfigureAwait(false); } else { throw new ArgumentException("please enter a image to upload :D"); } return(image.FileName); }
/// <summary> /// Initializes a new instance of the <see cref="FileManager"/> class. /// </summary> /// <param name="fileSystem">The fileSystem.</param> /// <param name="path">The path to file.</param> public FileManager(IFileSystem fileSystem, string path) { _type = FileManagerType.Nothing; _fileSystem = fileSystem; _path = path; }