public UploadThumb GetThumb(UploadedFile file, int width) { if (disposed) throw new ObjectDisposedException("UploadThumbGenerator"); if (!_thumbs.ContainsKey(file.TempKey)) _thumbs.Add(file.TempKey, new Dictionary<int, UploadThumb>()); var fileThumbs = _thumbs[file.TempKey]; if (fileThumbs.ContainsKey(width)) return (fileThumbs[width]); var thumb = new UploadThumb(); try { using (var image = Image.FromFile(file.TempFilePathAbsolute)) using (var resized = image.Width <= width ? image : ImageUtils.ResizeImage(image, width, false)) resized.Save(thumb.ThumbPathAbsolute, ImageFormat.Png); fileThumbs.Add(width, thumb); return thumb; } catch (OutOfMemoryException) { throw; } catch (Exception) { DeleteThumb(thumb); return null; } }
public UploadedFile HandleFile(HttpPostedFile postedFile) { var uploadedFile = new UploadedFile {Name = postedFile.FileName}; uploadedFile.TempFilePathAbsolute.EnsureDirectoryExists(); postedFile.SaveAs(uploadedFile.TempFilePathAbsolute); Files.Add(uploadedFile); return uploadedFile; }
public void DeleteThumbs(UploadedFile file) { if (!_thumbs.ContainsKey(file.TempKey)) return; var fileThumbs = _thumbs[file.TempKey]; foreach (var thumb in fileThumbs.Values) DeleteThumb(thumb); _thumbs.Remove(file.TempKey); }
public void RemoveFile(UploadedFile file) { if(!Files.Contains(file)) throw new ArgumentException("The file to be removed is not managed by this UploadManager"); DeleteFile(file); Files.Remove(file); ThumbGenerator.DeleteThumbs(file); }
private void DeleteFile(UploadedFile file) { File.Delete(file.TempFilePathAbsolute); }