/// <summary> /// 插入图片 /// </summary> /// <param name="pictureBinary">字节大小</param> /// <param name="mimeType">图片后缀</param> /// <param name="seoFilename">Seo 文件名</param> /// <param name="isNew">是否新图片</param> /// <param name="validateBinary">该值表示是否要提供验证图片的二进制</param> /// <returns>图片对象</returns> public virtual Picture InsertPicture(byte[] pictureBinary, string mimeType, string seoFilename, bool isNew, bool validateBinary = true) { mimeType = CommonHelper.EnsureNotNull(mimeType); mimeType = CommonHelper.EnsureMaximumLength(mimeType, 20); seoFilename = CommonHelper.EnsureMaximumLength(seoFilename, 100); if (validateBinary) pictureBinary = ValidatePicture(pictureBinary, mimeType); var picture = new Picture() { MimeType = mimeType, SeoFilename = seoFilename, IsNew = isNew, }; _pictureRepository.Insert(picture); SavePictureInFile(picture.Id, pictureBinary, mimeType); return picture; }
/// <summary> /// 删除图片 /// </summary> /// <param name="picture">Picture</param> public virtual void DeletePicture(Picture picture) { if (picture == null) throw new ArgumentNullException("picture"); //delete thumbs DeletePictureThumbs(picture); //delete from file system DeletePictureOnFileSystem(picture); //delete from database _pictureRepository.Delete(picture); }
/// <summary> /// 获取缩略图路径 /// </summary> /// <param name="picture">图片对象</param> /// <param name="targetSize">图片大小</param> /// <param name="showDefaultPicture">是否显示默认图像</param> /// <returns>缩略图路径</returns> public virtual string GetThumbLocalPath(Picture picture, int targetSize = 0, bool showDefaultPicture = true) { string url = GetPictureUrl(picture, targetSize, showDefaultPicture); if(String.IsNullOrEmpty(url)) return String.Empty; else return GetThumbLocalPath(Path.GetFileName(url)); }
/// <summary> /// 获取图片路径 /// </summary> /// <param name="picture">图片对象</param> /// <param name="targetSize">图片大小</param> /// <param name="showDefaultPicture">是否显示默认图像</param> /// <param name="defaultPictureType">D默认图片类型</param> /// <returns>图片路径</returns> public virtual string GetPictureUrl(Picture picture, int targetSize = 0, bool showDefaultPicture = true, PictureType defaultPictureType = PictureType.Entity) { string url = string.Empty; byte[] pictureBinary = null; if (picture != null) { pictureBinary = LoadPictureFromFile(picture.Id, picture.MimeType); } if (picture == null || pictureBinary == null || pictureBinary.Length == 0) { if(showDefaultPicture) { url = GetDefaultPictureUrl(targetSize, defaultPictureType); } return url; } string lastPart = GetFileExtensionFromMimeType(picture.MimeType); string thumbFileName; if (picture.IsNew) { DeletePictureThumbs(picture); //we do not validate picture binary here to ensure that no exception ("Parameter is not valid") will be thrown picture = UpdatePicture(picture.Id, pictureBinary, picture.MimeType, picture.SeoFilename, false, false); } lock (s_lock) { string seoFileName = picture.SeoFilename; // = GetPictureSeName(picture.SeoFilename); //just for sure if (targetSize == 0) { thumbFileName = !String.IsNullOrEmpty(seoFileName) ? string.Format("{0}_{1}.{2}", picture.Id.ToString("0000000"), seoFileName, lastPart) : string.Format("{0}.{1}", picture.Id.ToString("0000000"), lastPart); var thumbFilePath = GetThumbLocalPath(thumbFileName); if (!File.Exists(thumbFilePath)) { File.WriteAllBytes(thumbFilePath, pictureBinary); } } else { thumbFileName = !String.IsNullOrEmpty(seoFileName) ? string.Format("{0}_{1}_{2}.{3}", picture.Id.ToString("0000000"), seoFileName, targetSize, lastPart) : string.Format("{0}_{1}.{2}", picture.Id.ToString("0000000"), targetSize, lastPart); var thumbFilePath = GetThumbLocalPath(thumbFileName); if (!File.Exists(thumbFilePath)) { using (var stream = new MemoryStream(pictureBinary)) { Bitmap b = null; try { //try-catch to ensure that picture binary is really OK. Otherwise, we can get "Parameter is not valid" exception if binary is corrupted for some reasons b = new Bitmap(stream); } catch (ArgumentException exc) { //_logger.Error(string.Format("Error generating picture thumb. ID={0}", picture.Id), exc); } if (b == null) { //bitmap could not be loaded for some reasons return url; } var newSize = CalculateDimensions(b.Size, targetSize); var destStream = new MemoryStream(); ImageBuilder.Current.Build(b, destStream, new ResizeSettings() { Width = newSize.Width, Height = newSize.Height, Scale = ScaleMode.Both, //Quality = _mediaSettings.DefaultImageQuality }); var destBinary = destStream.ToArray(); File.WriteAllBytes(thumbFilePath, destBinary); b.Dispose(); } } } } url = GetThumbUrl(thumbFileName); return url; }
/// <summary> /// 删除缩略图 /// </summary> /// <param name="picture">图片对象</param> protected virtual void DeletePictureThumbs(Picture picture) { string filter = string.Format("{0}*.*", picture.Id.ToString("0000000")); var thumbDirectoryPath = _webHelper.MapPath("~/content/images/thumbs"); string[] currentFiles = System.IO.Directory.GetFiles(thumbDirectoryPath, filter, SearchOption.AllDirectories); foreach (string currentFileName in currentFiles) { var thumbFilePath = GetThumbLocalPath(currentFileName); File.Delete(thumbFilePath); } }
/// <summary> /// 删除图片 /// </summary> /// <param name="picture">图片对象</param> protected virtual void DeletePictureOnFileSystem(Picture picture) { if (picture == null) throw new ArgumentNullException("picture"); string lastPart = GetFileExtensionFromMimeType(picture.MimeType); string fileName = string.Format("{0}_0.{1}", picture.Id.ToString("0000000"), lastPart); string filePath = GetPictureLocalPath(fileName); if (File.Exists(filePath)) { File.Delete(filePath); } }