/// <summary> /// Rename item /// </summary> /// <param name="target">Target</param> /// <param name="name">Name</param> /// <returns>Response result</returns> RenameCommandResult IConnectorDriver.Rename(string target, string name) { // Create command result var commandResult = new RenameCommandResult(); // Parse target path var fullPath = ParsePath(target); // Add item to removed entries list commandResult.Removed.Add(target); // Proceed if it's a file or directory if (fullPath.IsDirectory()) { // Directory // Remove thumbnails fullPath.RemoveThumbs(); // Get parent path info var directoryInfo = fullPath.GetParentDirectory(); if (directoryInfo != null) { // Get new path var newPath = Path.Combine(directoryInfo.FullName, name); // Move path Directory.Move(((FileSystemVolumeDirectoryInfo)fullPath).Directory.FullName, newPath); // Add it to added entries list commandResult.Added.Add( DirectoryEntryObjectModel.Create(new DirectoryInfo(newPath), fullPath.Root)); } } else if (fullPath.IsFile()) { // File // Remove thumbnails fullPath.RemoveThumbs(); // Get new path var newPath = Path.Combine(((FileSystemVolumeFileInfo)fullPath).File.DirectoryName ?? string.Empty, name); // Move file File.Move(((FileSystemVolumeFileInfo)fullPath).File.FullName, newPath); // Add it to added entries list commandResult.Added.Add( FileEntryObjectModel.Create(new FileInfo(newPath), fullPath.Root)); } else { // Not supported throw new NotSupportedException(); } // Return result return(commandResult); }
/// <summary> /// Make file /// </summary> /// <param name="target">Target</param> /// <param name="name">Name</param> /// <returns>Response results</returns> MakeFileCommandResult IConnectorDriver.MakeFile(string target, string name) { // Create command result var commandResult = new MakeFileCommandResult(); // Parse target path var fullPath = ParseDirectoryPath(target); // Create file var newFileInfo = new FileInfo(Path.Combine(fullPath.Info.FullName, name)); newFileInfo.Create().Close(); // Add new file to response commandResult.Added.Add(FileEntryObjectModel.Create(newFileInfo, fullPath.Root)); // Return result return(commandResult); }
/// <summary> /// Put file content /// </summary> /// <param name="target">Target</param> /// <param name="content">Content</param> /// <returns>Response result</returns> PutCommandResult IConnectorDriver.Put(string target, string content) { // Create command result var commandResult = new PutCommandResult(); // Parse target file path var fullPath = ParseFilePath(target); // Write content using (var writer = new StreamWriter(fullPath.File.FullName, false)) { writer.Write(content); } // Add entry to changed items list commandResult.Changed.Add( FileEntryObjectModel.Create(fullPath.File, fullPath.Root)); // Return result return(commandResult); }
/// <summary> /// Search accessible files and directories in given volume /// </summary> /// <typeparam name="TVolume">Volume type</typeparam> /// <param name="volume">Volume</param> /// <param name="root">Root path</param> /// <param name="searchTerm">Search term</param> /// <returns>Result list</returns> public static IEnumerable <EntryObjectModel> SearchAccessibleFilesAndDirectories <TVolume>(this TVolume volume, string root, string searchTerm) where TVolume : FileSystemRootVolume { var entries = new List <EntryObjectModel>(); foreach (var directory in Directory.EnumerateDirectories(root).Where(m => m.Contains(searchTerm))) { entries.Add(DirectoryEntryObjectModel.Create(new DirectoryInfo(directory), volume)); } foreach (var file in Directory.EnumerateFiles(root).Where(m => m.Contains(searchTerm))) { // Check if file refers to an image or not if (ImagingUtils.CanProcessFile(file)) { // Add image to added entries entries.Add( ImageEntryObjectModel.Create(new FileInfo(file), volume)); } else { // Add file to added entries entries.Add( FileEntryObjectModel.Create(new FileInfo(file), volume)); } } foreach (var subDir in Directory.EnumerateDirectories(root)) { try { entries.AddRange(volume.SearchAccessibleFilesAndDirectories(subDir, searchTerm)); } catch (UnauthorizedAccessException) { // Do nothing } } return(entries); }
/// <summary> /// Open file /// </summary> /// <param name="target">Target</param> /// <param name="includeRootSubFolders">Include subfolders of root directories</param> /// <returns>Response result</returns> OpenCommandResult IConnectorDriver.Open(string target, bool includeRootSubFolders) { // Get directory target path info var fullPath = ParseDirectoryPath(target); // Create command result var commandResult = new OpenCommandResult( fullPath.CreateTargetEntryObjectModel(), new OptionsResultData(fullPath)); // Add visible files foreach (var fileItem in fullPath.Directory.GetVisibleFiles()) { // Check if file is supported for imaging if (ImagingUtils.CanProcessFile(fileItem)) { // Is supported // Add file item as image entry commandResult.Files.Add( ImageEntryObjectModel.Create(fileItem, fullPath.Root)); } else { // Not supported // Add file item as standard entry commandResult.Files.Add( FileEntryObjectModel.Create(fileItem, fullPath.Root)); } } // Add visible directories foreach (var directoryItem in fullPath.Directory.GetVisibleDirectories()) { commandResult.Files.Add(DirectoryEntryObjectModel.Create(directoryItem, fullPath.Root)); } // Return it return(commandResult); }
/// <summary> /// Upload files /// </summary> /// <param name="target">Target</param> /// <param name="files">Files</param> /// <returns>Response result</returns> UploadCommandResult IConnectorDriver.Upload(string target, IEnumerable <IFileStream> files) { // Parse target path var dest = ParsePath(target); // Create command result var commandResult = new UploadCommandResult(); // Normalize file streams var fileStreams = files as IFileStream[] ?? files.ToArray(); // Check if max upload size is set and that no files exceeds it if (dest.Root.MaxUploadSizeKb.HasValue && fileStreams.Any(x => (x.Stream.Length / 1024) > dest.Root.MaxUploadSizeKb)) { // Max upload size exceeded commandResult.SetMaxUploadFileSizeError(); // End processing return(commandResult); } // Loop files foreach (var file in fileStreams) { // Validate file name if (string.IsNullOrWhiteSpace(file.FileName)) { throw new ArgumentNullException(nameof(IFileStream.FileName)); } // Get path var path = new FileInfo(Path.Combine(dest.Info.FullName, Path.GetFileName(file.FileName))); // Check if it already exists if (path.Exists) { // Check if overwrite on upload is supported if (dest.Root.UploadOverwrite) { // If file already exist we rename the current file. // If upload is successful, delete temp file. Otherwise, we restore old file. var tmpPath = path.FullName + Guid.NewGuid(); // Save file var fileSaved = false; try { file.SaveAs(tmpPath); fileSaved = true; } catch (Exception) { } finally { // Check that file was saved correctly if (fileSaved) { // Delete file File.Delete(path.FullName); // Move file File.Move(tmpPath, path.FullName); // Remove thumbnails dest.RemoveThumbs(); } else { // Delete temporary file File.Delete(tmpPath); } } } else { // Ensure directy name is set if (string.IsNullOrEmpty(path.DirectoryName)) { throw new ArgumentNullException(nameof(FileInfo.DirectoryName)); } // Save file file.SaveAs(Path.Combine(path.DirectoryName, FileSystemUtils.GetDuplicatedName(path))); } } else { // Save file file.SaveAs(path.FullName); } // Check if file refers to an image or not if (ImagingUtils.CanProcessFile(path)) { // Add image to added entries commandResult.Added.Add( ImageEntryObjectModel.Create(new FileInfo(path.FullName), dest.Root)); } else { // Add file to added entries commandResult.Added.Add( FileEntryObjectModel.Create(new FileInfo(path.FullName), dest.Root)); } } // Return result return(commandResult); }
/// <summary> /// Paste items /// </summary> /// <param name="source">Source</param> /// <param name="dest">Destination</param> /// <param name="targets">Targets</param> /// <param name="isCut">Is cut</param> /// <returns>Response result</returns> PasteCommandResult IConnectorDriver.Paste(string source, string dest, IEnumerable <string> targets, bool isCut) { // Parse destination path var destPath = ParsePath(dest); // Create command result var commandResult = new PasteCommandResult(); // Loop target items foreach (var item in targets) { // Parse target item path, which will be the source var src = ParsePath(item); // Proceed if it's a file or directory if (src.IsDirectory()) { // Directory // Compute new directory var newDir = new DirectoryInfo(Path.Combine(destPath.Info.FullName, src.Info.Name)); // Check if it already exists if (newDir.Exists) { // Exists Directory.Delete(newDir.FullName, true); } // Check if content is being cut if (isCut) { // Remove thumbnails src.RemoveThumbs(); // Move directory src.GetDirectory().MoveTo(newDir.FullName); // Add to removed entries commandResult.Removed.Add(item); } else { // Copy directory FileSystemUtils.DirectoryCopy(src.GetDirectory(), newDir.FullName, true); } // Add it to added items commandResult.Added.Add( DirectoryEntryObjectModel.Create(newDir, destPath.Root)); } else if (src.IsFile()) { // File // Compute new file path var newFilePath = Path.Combine(destPath.Info.FullName, src.Info.Name); // Check if file exists if (File.Exists(newFilePath)) { // Exists File.Delete(newFilePath); } // Check if content is being cut if (isCut) { // Remove thumbnails src.RemoveThumbs(); // Move file File.Move(src.Info.FullName, newFilePath); // Add it to removed items commandResult.Removed.Add(item); } else { // Copy file File.Copy(src.Info.FullName, newFilePath); } // Check if file refers to an image or not if (ImagingUtils.CanProcessFile(newFilePath)) { // Add image to added entries commandResult.Added.Add( ImageEntryObjectModel.Create(new FileInfo(newFilePath), destPath.Root)); } else { // Add file to added entries commandResult.Added.Add( FileEntryObjectModel.Create(new FileInfo(newFilePath), destPath.Root)); } } else { // Not supported throw new NotSupportedException(); } } // Return result return(commandResult); }
/// <summary> /// Duplicate items /// </summary> /// <param name="targets">Targets</param> /// <returns>Response result</returns> DuplicateCommandResult IConnectorDriver.Duplicate(IEnumerable <string> targets) { // Create command result var commandResult = new DuplicateCommandResult(); // Loop targets foreach (var targetItem in targets) { // Parse target path var fullPath = ParsePath(targetItem); // Proceed if it's a file or directory if (fullPath.IsDirectory()) { // Directory // Remove thumbnails fullPath.RemoveThumbs(); // Get path info var parentPath = fullPath.GetParentDirectory().FullName; var name = fullPath.GetDirectory().Name; // Compute new name var newName = $@"{parentPath}\{name} copy"; // Check if directory already exists if (!Directory.Exists(newName)) { // Doesn't exist FileSystemUtils.DirectoryCopy(fullPath.GetDirectory(), newName, true); } else { // Already exists, create numbered copy var newNameFound = false; for (var i = 1; i < 100; i++) { // Compute new name newName = $@"{parentPath}\{name} copy {i}"; // Test that it doesn't exist if (!Directory.Exists(newName)) { FileSystemUtils.DirectoryCopy(fullPath.GetDirectory(), newName, true); newNameFound = true; break; } } // Check if new name was found if (!newNameFound) { throw new ELFinderNewNameSelectionException($@"{parentPath}\{name} copy"); } } // Add entry to added items commandResult.Added.Add( DirectoryEntryObjectModel.Create(new DirectoryInfo(newName), fullPath.Root)); } else if (fullPath.IsFile()) { // File // Remove thumbnails fullPath.RemoveThumbs(); // Get path info var parentPath = fullPath.GetDirectory().FullName; var name = fullPath.Info.Name.Substring(0, fullPath.Info.Name.Length - fullPath.Info.Extension.Length); var ext = fullPath.Info.Extension; // Compute new name var newName = $@"{parentPath}\{name} copy{ext}"; // Check if file already exists if (!File.Exists(newName)) { // Doesn't exist ((FileSystemVolumeFileInfo)fullPath).File.CopyTo(newName); } else { // Already exists, create numbered copy var newNameFound = false; for (var i = 1; i < 100; i++) { // Compute new name newName = $@"{parentPath}\{name} copy {i}{ext}"; // Test that it doesn't exist if (!File.Exists(newName)) { ((FileSystemVolumeFileInfo)fullPath).File.CopyTo(newName); newNameFound = true; break; } } // Check if new name was found if (!newNameFound) { throw new ELFinderNewNameSelectionException($@"{parentPath}\{name} copy{ext}"); } } // Check if file refers to an image or not if (ImagingUtils.CanProcessFile(newName)) { // Add image to added entries commandResult.Added.Add( ImageEntryObjectModel.Create(new FileInfo(newName), fullPath.Root)); } else { // Add file to added entries commandResult.Added.Add( FileEntryObjectModel.Create(new FileInfo(newName), fullPath.Root)); } } else { // Not supported throw new NotSupportedException(); } } // Return result return(commandResult); }
/// <summary> /// Initialize file/directory open /// </summary> /// <param name="target">Target</param> /// <returns>Response result</returns> InitCommandResult IConnectorDriver.Init(string target) { // Declare target directory path info FileSystemVolumeDirectoryInfo fullPath; // Check if target was defined if (string.IsNullOrEmpty(target)) { // Target not defined // Get startup volume var startupVolume = RootVolumes.GetStartupVolume(); // Get directory target path info fullPath = new FileSystemVolumeDirectoryInfo( startupVolume, startupVolume.StartDirectory ?? startupVolume.Directory); } else { // Target defined // Get directory target path info fullPath = ParseDirectoryPath(target); } // Create command result var commandResult = new InitCommandResult( fullPath.CreateTargetEntryObjectModel(), new OptionsResultData(fullPath)); // Add visible files foreach (var fileItem in fullPath.Directory.GetVisibleFiles()) { // Check if file is supported for imaging if (ImagingUtils.CanProcessFile(fileItem)) { // Is supported // Add file item as image entry commandResult.Files.Add( ImageEntryObjectModel.Create(fileItem, fullPath.Root)); } else { // Not supported // Add file item as standard entry commandResult.Files.Add( FileEntryObjectModel.Create(fileItem, fullPath.Root)); } } // Add visible directories foreach (var directoryItem in fullPath.Directory.GetVisibleDirectories()) { commandResult.Files.Add(DirectoryEntryObjectModel.Create(directoryItem, fullPath.Root)); } // Add root directories foreach (var rootVolume in RootVolumes) { commandResult.Files.Add(RootDirectoryEntryObjectModel.Create(rootVolume.Directory, rootVolume)); } // Add root directories, if different from root if (!fullPath.IsDirectorySameAsRoot) { foreach (var directoryItem in fullPath.Root.Directory.GetVisibleDirectories()) { commandResult.Files.Add(DirectoryEntryObjectModel.Create(directoryItem, fullPath.Root)); } } // Set max upload size if (fullPath.Root.MaxUploadSizeKb.HasValue) { commandResult.UploadMaxSize = $"{fullPath.Root.MaxUploadSizeKb.Value}K"; } // Return it return(commandResult); }