protected virtual IFileObject GetUniqueFile(IFileSystem fs, string fileName) { int fileNum = 0; IFileObject uniqueFile = null; do { fileNum++; var extIdx = fileName.LastIndexOf('.'); var newFileName = extIdx >= 0 ? String.Format("{0}{1}{2}", fileName.Substring(0, extIdx), fileNum, fileName.Substring(extIdx)) : fileName + fileNum.ToString(); uniqueFile = fs.ResolveFile(newFileName); } while (uniqueFile.Exists() && fileNum < 100); if (uniqueFile.Exists()) { var extIdx = fileName.LastIndexOf('.'); var uniqueSuffix = Guid.NewGuid().ToString(); uniqueFile = fs.ResolveFile( extIdx >= 0 ? fileName.Substring(0, extIdx) + uniqueSuffix + fileName.Substring(extIdx) : fileName + uniqueSuffix); } return(uniqueFile); }
public virtual IFileObject SaveFile(IFileSystem fs, InputFileInfo inputFile) { var originalFileName = Path.GetFileName(inputFile.FileName); var fileName = Path.Combine(inputFile.Folder, originalFileName); log.Write(LogEvent.Debug, "Saving file: {0}", fileName); var uploadFile = fs.ResolveFile(fileName); if (uploadFile.Exists()) { if (inputFile.Overwrite) { uploadFile.Delete(); } else { uploadFile = GetUniqueFile(fs, fileName); } } // special handling for images if (inputFile.ForceImageFormat != null || inputFile.ImageMaxWidth > 0 || inputFile.ImageMaxHeight > 0) { var imgFormat = String.IsNullOrEmpty(inputFile.ForceImageFormat)? ImageFormat.Png : ImageUtils.ResolveImageFormat(inputFile.ForceImageFormat); var imgFormatExt = ImageUtils.GetImageFormatExtension(imgFormat); if (imgFormatExt != (Path.GetExtension(uploadFile.Name) ?? String.Empty).ToLower()) { uploadFile = GetUniqueFile(fs, Path.Combine(inputFile.Folder, Path.GetFileNameWithoutExtension(originalFileName) + imgFormatExt)); } uploadFile.CreateFile(); using (var outputStream = uploadFile.Content.GetStream(FileAccess.Write)) { ImageUtils.ResizeImage( inputFile.InputStream, outputStream, imgFormat, inputFile.ImageMaxWidth, inputFile.ImageMaxHeight, true ); } } else { uploadFile.CopyFrom(inputFile.InputStream); } return(uploadFile); }
public ShadowNodeContent(ShadowFile shadowFile, IFile file, IFileSystem tempFileSystem) { var buffer = new StringBuilder(file.Address.Uri.Length * 3); this.shadowFile = shadowFile; this.tempFileSystem = tempFileSystem; this.file = tempFileSystem.ResolveFile(GenerateName()); buffer.Append(ComputeHash(file.Address.Uri).TrimRight('=')); var value = (string)file.Address.QueryValues["length"]; if (value != null) { this.targetLength = Convert.ToInt64(value); buffer.Append('_').Append(value); } value = (string)file.Address.QueryValues["md5sum"]; if (value != null) { this.targetMd5 = Convert.FromBase64String(value); buffer.Append('_').Append(value); } value = (string)file.Address.QueryValues["creationtime"]; if (value != null) { this.hasCreationTime = true; this.creationTime = Convert.ToDateTime(value); buffer.Append('_').Append(this.creationTime.Ticks.ToString()); } value = (string)file.Address.QueryValues["writetime"]; if (value != null) { this.hasLastWriteTime = true; this.lastWriteTime = Convert.ToDateTime(value); buffer.Append('_').Append(this.lastWriteTime.Ticks.ToString()); } value = buffer.ToString(); }
protected IFileObject GetThumbnail(IFileObject originalFile, IFileSystem filesystem, string width, string height) { var resizeWidth = AssertHelper.IsFuzzyEmpty(width) ? 0 : Convert.ToInt32(width); var resizeHeight = AssertHelper.IsFuzzyEmpty(height) ? 0 : Convert.ToInt32(height); if (resizeWidth == 0 && resizeHeight == 0) { return(originalFile); } var thumbnailFileName = String.Format("{0}-thumbnail{1}x{2}{3}", Path.Combine(Path.GetDirectoryName(originalFile.Name), Path.GetFileNameWithoutExtension(originalFile.Name)), resizeWidth, resizeHeight, Path.GetExtension(originalFile.Name) ); var thumbnailFile = filesystem.ResolveFile(thumbnailFileName); return(!thumbnailFile.Exists() ? ImageHelper.SaveAndResizeImage(originalFile.GetContent().InputStream, filesystem, thumbnailFile, resizeWidth, resizeHeight) : thumbnailFile); }
private IFileObject GetFile(string path) { return(path == "\\" ? fileSystem.Root : fileSystem.ResolveFile("D:" + path)); }
public static IFileObject SaveAndResizeImage(Stream input, IFileSystem fs, IFileObject file, int maxWidth, int maxHeight, ImageFormat saveAsFormat ) { Image img; MemoryStream imgSrcStream = new MemoryStream(); byte[] buf = new byte[1024*50]; int bufRead = 0; do { bufRead = input.Read(buf, 0, buf.Length); if (bufRead>0) imgSrcStream.Write(buf, 0, bufRead); } while (bufRead>0); imgSrcStream.Position = 0; try { img = Image.FromStream(imgSrcStream); } catch (Exception ex) { throw new Exception( WebManager.GetLabel("Invalid image format") ); } if (img.Size.Width==0 || img.Size.Height==0) throw new Exception( WebManager.GetLabel("Invalid image size") ); var sizeIsWidthOk = (maxWidth<=0 || img.Size.Width<=maxWidth); var sizeIsHeightOk = (maxHeight<=0 || img.Size.Height<=maxHeight); var sizeIsOk = sizeIsWidthOk && sizeIsHeightOk; var originalImgFmt = ResolveImageFormat( Path.GetExtension(file.Name) ) ?? ImageFormat.Bmp; var formatIsOk = (saveAsFormat==null && !originalImgFmt.Equals(ImageFormat.Bmp) && !originalImgFmt.Equals(ImageFormat.Tiff) ) || originalImgFmt.Equals(saveAsFormat); if (!formatIsOk || !sizeIsOk ) { var saveAsFormatResolved = saveAsFormat!=null ? saveAsFormat : (originalImgFmt==ImageFormat.Jpeg?ImageFormat.Jpeg:ImageFormat.Png); var newFmtExtension = GetImageFormatExtension(saveAsFormatResolved); var newFile = fs.ResolveFile( file.Name + (Path.GetExtension(file.Name).ToLower()==newFmtExtension ? String.Empty : newFmtExtension) ); newFile.CreateFile(); if (!sizeIsOk) { var newWidth = img.Size.Width; var newHeight = img.Size.Height; if (!sizeIsWidthOk) { newWidth = maxWidth; newHeight = (int) Math.Floor( ((double)img.Size.Height)*( ((double)maxWidth)/((double)img.Size.Width) ) ); if ( maxHeight<0 || newHeight<=maxHeight ) sizeIsHeightOk = true; } if (!sizeIsHeightOk) { newHeight = maxHeight; newWidth = (int) Math.Floor( ((double)img.Size.Width)*( ((double)maxHeight)/((double)img.Size.Height) ) ); } var resizedBitmap = new Bitmap(img, newWidth, newHeight); var imageProps = img.PropertyItems; foreach (PropertyItem propItem in imageProps){ resizedBitmap.SetPropertyItem(propItem); } SaveImage(resizedBitmap, newFile.GetContent().OutputStream, saveAsFormatResolved); } else { SaveImage(img, newFile.GetContent().OutputStream, saveAsFormatResolved ); } newFile.Close(); return newFile; } file.CreateFile(); imgSrcStream.Position = 0; file.CopyFrom( imgSrcStream ); file.Close(); return file; }
public static IFileObject SaveAndResizeImage(Stream input, IFileSystem fs, IFileObject file, int maxWidth, int maxHeight, ImageFormat saveAsFormat) { Image img; MemoryStream imgSrcStream = new MemoryStream(); byte[] buf = new byte[1024 * 50]; int bufRead = 0; do { bufRead = input.Read(buf, 0, buf.Length); if (bufRead > 0) { imgSrcStream.Write(buf, 0, bufRead); } } while (bufRead > 0); imgSrcStream.Position = 0; try { img = Image.FromStream(imgSrcStream); } catch (Exception ex) { throw new Exception(WebManager.GetLabel("Invalid image format")); } if (img.Size.Width == 0 || img.Size.Height == 0) { throw new Exception(WebManager.GetLabel("Invalid image size")); } var sizeIsWidthOk = (maxWidth <= 0 || img.Size.Width <= maxWidth); var sizeIsHeightOk = (maxHeight <= 0 || img.Size.Height <= maxHeight); var sizeIsOk = sizeIsWidthOk && sizeIsHeightOk; var originalImgFmt = ResolveImageFormat(Path.GetExtension(file.Name)) ?? ImageFormat.Bmp; var formatIsOk = (saveAsFormat == null && !originalImgFmt.Equals(ImageFormat.Bmp) && !originalImgFmt.Equals(ImageFormat.Tiff)) || originalImgFmt.Equals(saveAsFormat); if (!formatIsOk || !sizeIsOk) { var saveAsFormatResolved = saveAsFormat != null ? saveAsFormat : (originalImgFmt == ImageFormat.Jpeg?ImageFormat.Jpeg:ImageFormat.Png); var newFmtExtension = GetImageFormatExtension(saveAsFormatResolved); var newFile = fs.ResolveFile(file.Name + (Path.GetExtension(file.Name).ToLower() == newFmtExtension ? String.Empty : newFmtExtension)); newFile.CreateFile(); if (!sizeIsOk) { var newWidth = img.Size.Width; var newHeight = img.Size.Height; if (!sizeIsWidthOk) { newWidth = maxWidth; newHeight = (int)Math.Floor(((double)img.Size.Height) * (((double)maxWidth) / ((double)img.Size.Width))); if (maxHeight < 0 || newHeight <= maxHeight) { sizeIsHeightOk = true; } } if (!sizeIsHeightOk) { newHeight = maxHeight; newWidth = (int)Math.Floor(((double)img.Size.Width) * (((double)maxHeight) / ((double)img.Size.Height))); } var resizedBitmap = new Bitmap(img, newWidth, newHeight); var imageProps = img.PropertyItems; foreach (PropertyItem propItem in imageProps) { resizedBitmap.SetPropertyItem(propItem); } SaveImage(resizedBitmap, newFile.GetContent().OutputStream, saveAsFormatResolved); } else { SaveImage(img, newFile.GetContent().OutputStream, saveAsFormatResolved); } newFile.Close(); return(newFile); } file.CreateFile(); imgSrcStream.Position = 0; file.CopyFrom(imgSrcStream); file.Close(); return(file); }
protected void prepeare(IFileSystem fileSystem) { // create folder IFileObject testFolder = fileSystem.ResolveFile("test"); testFolder.CreateFolder(); // create 10 files for (int i = 0; i < 10; i++) { string fName = String.Format("test/test{0}.{1}", i, i % 2 == 0 ? "txt" : "doc"); IFileObject testFile = fileSystem.ResolveFile(fName); testFile.CreateFile(); StreamWriter streamWr = new StreamWriter(testFile.Content.GetStream(FileAccess.Write)); streamWr.Write("This is test content #" + i.ToString()); streamWr.Close(); } // GetChildren if (testFolder.GetChildren().Length != 10) { throw new Exception("GetChildren failed"); } // create another dir with subdir IFileObject testTest2Folder = fileSystem.ResolveFile("test2/test/"); testTest2Folder.CopyFrom(testFolder); testTest2Folder.Parent.CopyFrom(testFolder); // FindFiles IFileObject[] txtTest2Files = testTest2Folder.Parent.FindFiles(new MaskFileSelector("*.txt")); if (txtTest2Files.Length != 10) { throw new Exception("FindFiles failed"); } foreach (IFileObject f in txtTest2Files) { f.Delete(); } if (testTest2Folder.GetChildren().Length != 5) { throw new Exception("Delete failed"); } // check copied file content IFileObject test1docFile = fileSystem.ResolveFile("test2/test/test1.doc"); if (!test1docFile.Exists()) { throw new Exception("ResolveFile failed"); } StreamReader rdr = new StreamReader(test1docFile.Content.GetStream(FileAccess.Read)); string content = rdr.ReadToEnd(); rdr.Close(); Assert.AreEqual("This is test content #1", content); // deep tree copy test IFileObject test3Folder = fileSystem.ResolveFile("test3"); test3Folder.CopyFrom(fileSystem.ResolveFile("test2")); // count doc files if (fileSystem.Root.FindFiles(new MaskFileSelector("*.doc")).Length != 25) { throw new Exception("CopyFrom (subtree) or FindFiles failed"); } }