/// <summary> /// <see cref="IFileObject.CopyFrom"/> /// </summary> protected virtual void CopyFrom(IFileObject[] srcEntries) { if (Type == FileType.File) { Delete(); } if (Type == FileType.Imaginary) { CreateFolder(); } foreach (IFileObject srcFile in srcEntries) { string destFileName = Path.Combine(Name, Path.GetFileName(srcFile.Name)); IFileObject destFile = LocalFs.ResolveFile(destFileName); destFile.CopyFrom(srcFile); } }
public void CopyFrom(IFileObject[] srcEntries) { if (Type == FileType.File) { Delete(); } if (Type == FileType.Imaginary) { CreateFolder(); } foreach (IFileObject srcFile in srcEntries) { string destFileName = Path.Combine(DalcFs.UnformatPath(Name), Path.GetFileName(DalcFs.UnformatPath(srcFile.Name))); IFileObject destFile = DalcFs.ResolveFile(destFileName); destFile.CopyFrom(srcFile); } }
/// <summary> /// <see cref="IFileObject.MoveTo"/> /// </summary> public virtual void MoveTo(IFileObject destFile) { // raise 'before move' LocalFs.OnFileMoving(this, destFile); try { if (destFile is LocalFile) { // delete destination file if (destFile.Type != FileType.Imaginary) { destFile.Delete(); } // use fast API move functions... if (Type == FileType.File) { File.Move(LocalName, ((LocalFile)destFile).LocalName); } if (Type == FileType.Folder) { Directory.Move(LocalName, ((LocalFile)destFile).LocalName); } } else { // copy-delete destFile.CopyFrom(this); this.Delete(); } } catch (Exception ex) { // raise 'error' LocalFs.OnFileError(this, ex); throw new FileSystemException(ex.Message, this, ex); } // raise 'after move' LocalFs.OnFileMoved(this, destFile); }
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; }
/// <summary> /// <see cref="IFileObject.MoveTo"/> /// </summary> public virtual void MoveTo(IFileObject destFile) { // copy-delete destFile.CopyFrom(this); this.Delete(); }
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"); } }