public string GetPictureUrl(Media picture, int width = 0, int height = 0, bool returnDefaultIfNotFound = false) { //check if picture is not null if (picture == null || picture.Id == 0) { return(string.Empty); } if (_mediaSettings.PictureSaveLocation == MediaSaveLocation.Database) { //todo: implement database storage } var expectedFile = FileHelpers.GetPictureFileNameWithSize(picture.LocalPath, width, height); var expectedFileSystemPath = ServerHelper.GetLocalPathFromRelativePath(expectedFile); if (!File.Exists(expectedFileSystemPath)) { //we need to create the file with required dimensions var fileSystemPathForOriginalImage = ServerHelper.GetLocalPathFromRelativePath(picture.LocalPath); //image format var imageFormat = PictureUtility.GetImageFormatFromContentType(picture.MimeType); //save resized image _imageProcessor.WriteResizedImage(fileSystemPathForOriginalImage, expectedFileSystemPath, width, height, imageFormat); } //return the image url var imageServeUrl = expectedFile.Replace("~", _generalSettings.ImageServerDomain); return(imageServeUrl); }
public void WritePictureBytes(Media picture, MediaSaveLocation pictureSaveLocation) { //we need to save the file on file system if (picture.Binary == null || !picture.Binary.Any()) { throw new mobSocialException("Can't write empty bytes for picture"); } if (pictureSaveLocation == MediaSaveLocation.FileSystem) { //get the directory path from the relative path var directoryPath = ServerHelper.GetLocalPathFromRelativePath(_mediaSettings.PictureSavePath); var fileExtension = PathUtility.GetFileExtensionFromContentType(picture.MimeType); if (string.IsNullOrEmpty(picture.SystemName)) { picture.SystemName = $"{Guid.NewGuid().ToString("n")}"; } var proposedFileName = $"{picture.SystemName}{fileExtension}"; var filePath = PathUtility.GetFileSavePath(directoryPath, proposedFileName); var imageFormat = PictureUtility.GetImageFormatFromContentType(picture.MimeType); _imageProcessor.WriteBytesToImage(picture.Binary, filePath, imageFormat); //clear bytes picture.Binary = null; picture.LocalPath = filePath; picture.ThumbnailPath = ServerHelper.GetRelativePathFromLocalPath(filePath); } }
public string GetPictureUrl(Media picture, int width = 0, int height = 0, bool returnDefaultIfNotFound = false) { //check if picture is not null if (picture == null || picture.Id == 0) { return(string.Empty); } var expectedFile = picture.LocalPath; var expectedFileSystemPath = ServerHelper.GetLocalPathFromRelativePath(expectedFile); if (!File.Exists(expectedFileSystemPath)) { //we need to create the file with required dimensions var fileSystemPathForOriginalImage = ServerHelper.GetLocalPathFromRelativePath(picture.LocalPath); //image format var imageFormat = PictureUtility.GetImageFormatFromContentType(picture.MimeType); //save resized image _imageProcessor.WriteResizedImage(fileSystemPathForOriginalImage, expectedFileSystemPath, width, height, imageFormat); } var storeUrl = _webHelper.GetStoreLocation(); //return the image url var imageServeUrl = expectedFile.Replace("~", storeUrl); return(imageServeUrl); }