示例#1
0
        public virtual void DeletePicture(Picture picture)
        {
            if (picture == null)
                throw new ArgumentNullException("picture");

            //delete thumbs
            DeletePictureThumbs(picture);

            //delete from database
            _pictureRepository.Delete(picture);
        }
示例#2
0
 protected virtual void DeletePictureThumbs(Picture picture)
 {
     string filter = string.Format("{0}*.*", picture.Id.ToString("0000000"));
     var thumbDirectoryPath = GetThumbsDirectory();
     string[] currentFiles = System.IO.Directory.GetFiles(thumbDirectoryPath, filter, SearchOption.AllDirectories);
     foreach (string currentFileName in currentFiles)
     {
         var thumbFilePath = GetThumbLocalPath(currentFileName);
         if (File.Exists(thumbFilePath))
             File.Delete(thumbFilePath);
     }
 }
示例#3
0
        protected virtual void DeletePictureOnFileSystem(Picture picture)
        {
            if (picture == null)
                throw new ArgumentNullException("picture");

            string lastPart = GetFileExtensionFromMimeType(picture.Mime);
            string fileName = string.Format("{0}_0.{1}", picture.Id.ToString("0000000"), lastPart);
            string filePath = GetPictureLocalPath(fileName);
            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }
        }
示例#4
0
        public virtual byte[] LoadPictureBinary(Picture picture)
        {
            if (picture == null)
                throw new ArgumentNullException("picture");

            return picture.PictureBinary;
        }
示例#5
0
        public virtual Picture InsertPicture(byte[] pictureBinary, string mime, bool isNew)
        {
            mime = CommonHelper.EnsureNotNull(mime);
            mime = CommonHelper.EnsureMaximumLength(mime, 20);

            var picture = new Picture() { PictureBinary = pictureBinary,Mime = mime, IsNew = isNew };
            _pictureRepository.Insert(picture);
            return picture;
        }
示例#6
0
 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));
 }
示例#7
0
        public virtual string GetPictureUrl(Picture picture, int targetSize = 0, bool showDefaultPicture = true)
        {
            string url = string.Empty;
            byte[] pictureBinary = null;
            if (picture != null)
                pictureBinary = LoadPictureBinary(picture);
            if (picture == null || pictureBinary == null || pictureBinary.Length == 0)
            {
                if (showDefaultPicture)
                {
                    url = GetDefaultPictureUrl(targetSize);
                }
                return url;
            }

            string lastPart = GetFileExtensionFromMimeType(picture.Mime);
            string thumbFileName;
            if (picture.IsNew)
            {
                DeletePictureThumbs(picture);
                picture = UpdatePicture(picture.Id, pictureBinary, picture.Mime, false);
            }
            lock (s_lock)
            {
                if (targetSize == 0)
                {
                    thumbFileName = string.Format("{0}.{1}", picture.Id.ToString("0000000"), lastPart);
                    var thumbFilePath = GetThumbLocalPath(thumbFileName);
                    if (!File.Exists(thumbFilePath))
                    {
                        File.WriteAllBytes(thumbFilePath, pictureBinary);
                    }
                }
                else
                {
                    thumbFileName = 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
                            {
                                b = new Bitmap(stream);
                            }
                            catch (ArgumentException)
                            {
                                // Logger.Error
                            }
                            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
                            });
                            var destBinary = destStream.ToArray();
                            File.WriteAllBytes(thumbFilePath, destBinary);

                            b.Dispose();
                        }
                    }
                }
            }
            url = GetThumbUrl(thumbFileName);
            return url;
        }