/// <summary> /// Writes the specified byte array to the file. If the target file already exists, it is overwritten. /// </summary> /// <param name="file">IFileObject instance</param> /// <param name="bytes">The bytes to write to the file.</param> public static void WriteAllBytes(this IFileObject file, byte[] bytes) { if (file.Type == FileType.File) { file.Delete(); file.CreateFile(); } using (var fs = file.Content.GetStream(FileAccess.Write)) { fs.Write(bytes, 0, bytes.Length); } }
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"); } }