public Stream GetAvatarStream(string key) { if (string.IsNullOrEmpty(key)) { return(null); } var avatar = _avatarDirectoryInfo.GetFile(key + ".jpg"); if (avatar.Exists) { return(avatar.Open(FileMode.Open)); } return(null); }
public string UploadAvatar(IFormFile file, string key) { if (file.Length >= 300000) { throw new Exception("Uploaded image may not exceed 300 kb, please upload a smaller image."); } try { using (var readStream = file.OpenReadStream()) { using (var img = Image.FromStream(readStream)) { if (!img.RawFormat.Equals(ImageFormat.Jpeg) && !img.RawFormat.Equals(ImageFormat.Png)) { throw new Exception("Uploaded file is not recognized as an image."); } var fileName = key + ".png"; try { // Check if previous avatar exists var avatar = _avatarDirectoryInfo.GetFile(fileName); if (avatar.Exists) { // delete the file avatar.Delete(); } // finally, build the image AvatarBuilder.Build(readStream, fileName); } catch (Exception ex) { throw new Exception("Uploaded file is not recognized as a valid image.", ex); } return(key); } } } catch (Exception) { throw new Exception("Uploaded file is not recognized as an image."); } }
/// <summary> /// Gets the full path of the file for the specified destination. /// </summary> /// <param name="fileResult"> /// The file result to get the path for. /// </param> /// <param name="destination"> /// The destination directory. /// </param> /// <returns> /// The full path of the file. /// </returns> public IFileInfo GetFullPath(FileResult fileResult, IDirectoryInfo destination) { if (fileResult.Incomplete) { throw new InvalidOperationException("There is not enough data to get the file path."); } return(destination.GetFile(FormatOutputPath(fileResult))); }
public string UploadImage(Image image) { // TODO: maybe a global lock while we upload this image? var fileName = RandomKey() + ".jpg"; while (_thumbnailDirectory.FileExists(fileName)) { fileName = RandomKey() + ".jpg"; } try { _thumbnailDirectory.GetFile(fileName).Open(FileMode.Create, stream => image.Save(stream, ImageFormat.Jpeg)); } catch (Exception) { try { // if there was any issue, try to delete the file, if we created if (_thumbnailDirectory.FileExists(fileName)) { _thumbnailDirectory.DeleteFile(fileName); } } catch (Exception) { // the error was more fundamental. // don't do anything with this exception. // let the previous exception be the real one thrown. } throw; } return(fileName); }
/// <summary> /// Gets the full path of the file for the specified destination. /// </summary> /// <param name="destination"> /// The destination directory. /// </param> /// <param name="provider"> /// The storage provider. /// </param> /// <returns> /// The full path of the file. /// </returns> internal IFileInfo GetFullPath(IDirectoryInfo destination, IStorageProvider provider) { if (this.Incomplete) { throw new InvalidOperationException("There is not enough data to get the file path."); } return destination.GetFile(this.FormatOutputPath(this.GetOuptutFormat(provider))); }