Ejemplo n.º 1
0
 void RaisePostBackEvent(string eventArgument)
 {
     if (eventArgument == "Upload")
     {
         HttpPostedFile uploadedFile = Page.Request.Files [ClientID + "_Upload"];
         if (uploadedFile != null && uploadedFile.ContentLength > 0)
         {
             FileManagerItemInfo dir = GetCurrentDirectory();
             Controller.ProcessFileUpload(dir, uploadedFile);
         }
     }
     else if (eventArgument.StartsWith("Toolbar:", StringComparison.Ordinal))
     {
         int i = int.Parse(eventArgument.Substring(8));
         CustomToolbarButton button = CustomToolbarButtons [i];
         OnToolbarCommand(new CommandEventArgs(button.CommandName, button.CommandArgument));
     }
 }
        private string ProcessOpenCommand(FileManagerItemInfo item)
        {
            StringBuilder sb = new StringBuilder();

            if (ClientOpenItemFunction.Length > 0)
            {
                sb.AppendLine("var href = '" + EncodeURIComponent(item.VirtualPath) + "';");
                sb.AppendLine(ClientOpenItemFunction + "(decodeURIComponent(href));");
            }
            else
            {
                sb.AppendLine("var href = '" + EncodeURIComponent(item.VirtualPath) + "';");
                sb.AppendLine("window.open(encodeURI(decodeURIComponent(href)).replace('+', '%2b'),'_blank');");
            }
            return sb.ToString();
        }
        private string ProcessNewFolder(FileManagerItemInfo destDir)
        {
            NewFolderCancelEventArgs cancelArg = new NewFolderCancelEventArgs();
            cancelArg.DestinationDirectory = destDir;
            OnNewFolderCreating(cancelArg);

            if (cancelArg.Cancel)
            {
                return ClientMessageEventReference(cancelArg.ClientMessage);
            }

            string folderName = GetResourceString("New_Folder_Name", "New Folder");

            string newFolderName = GetNotDuplicatedFolderName(destDir, folderName);

            FileManagerItemInfo itemInfo = ResolveFileManagerItemInfo(VirtualPathUtility.AppendTrailingSlash(destDir.FileManagerPath) + newFolderName);
            itemInfo.EnsureDirectoryExists();

            NewFolderEventArgs arg = new NewFolderEventArgs();
            arg.NewFolder = itemInfo;
            OnNewFolderCreated(arg);

            StringBuilder sb = new StringBuilder();
            AddFolderTreeRefreshEventReference(sb, destDir);
            sb.AppendLine(ClientRefreshEventReference);
            return sb.ToString();
        }
        internal bool ProcessFileUpload(FileManagerItemInfo destDir, System.Web.HttpPostedFile uploadedFile, out string script)
        {
            UploadFileCancelEventArgs cancelArg = new UploadFileCancelEventArgs();
            cancelArg.DestinationDirectory = destDir;
            cancelArg.PostedFile = uploadedFile;
            OnFileUploading(cancelArg);

            if (cancelArg.Cancel)
            {
                script = ClientMessageEventReference(cancelArg.ClientMessage);
                return false;
            }

            string saveName = cancelArg.SaveName;
            string fileNameWithoutExtension = saveName.Substring(0, saveName.LastIndexOf('.'));
            string extension = saveName.Substring(saveName.LastIndexOf('.'));

            string ext = extension.ToLower(CultureInfo.InvariantCulture).TrimStart('.');
            if (HiddenFilesArray.Contains(ext) ||
                    ProhibitedFilesArray.Contains(ext))
            {
                script = ClientMessageEventReference(null);
                return false;
            }

            string newFileName = GetNotDuplicatedFileName(destDir, fileNameWithoutExtension, extension);
            FileManagerItemInfo itemInfo = ResolveFileManagerItemInfo(VirtualPathUtility.AppendTrailingSlash(destDir.FileManagerPath) + newFileName);

            uploadedFile.SaveAs(itemInfo.PhysicalPath);

            UploadFileEventArgs arg = new UploadFileEventArgs();
            arg.UploadedFile = itemInfo;
            OnFileUploaded(arg);

            script = ClientRefreshEventReference;
            return true;
        }
 private static string GetNotDuplicatedFolderName(FileManagerItemInfo destDir, string folderName)
 {
     string newFolderName = folderName;
     int i = 1;
     string originNewFolderName = folderName;
     while (Directory.Exists(Path.Combine(destDir.PhysicalPath, newFolderName)))
     {
         newFolderName = originNewFolderName + "(" + i + ")";
         i++;
     }
     return newFolderName;
 }
 private bool ProcessSelectedItemsDelete(FileManagerItemInfo[] items)
 {
     bool b = false;
     foreach (FileManagerItemInfo item in items)
     {
         if (item.File.Exists)
             item.File.Delete();
         else if (item.Directory.Exists)
         {
             b = true;
             item.Directory.Delete(true);
         }
     }
     return b;
 }
        internal string GetItemThumbnailImage(FileSystemInfo fsi, FileManagerItemInfo currentDirectory)
        {
            FileInfo file = fsi as FileInfo;
            if (file == null)
                return GetFolderLargeImage((DirectoryInfo)fsi);

            if (IsImage(file))
                return ResolveUrl("~/" + ThumbnailHandler + "?" + HttpUtility.UrlEncode(currentDirectory.VirtualPath + "/" + file.Name));

            return GetFileLargeImage(file);
        }
 void AddFolderTreeRequireRefreshEventReference(StringBuilder sb, FileManagerItemInfo srcInfo, FileManagerItemInfo destInfo)
 {
     sb.AppendLine("var folderTree = window['WFM_' + context.ClientID + '_FolderTree'];");
     sb.AppendLine("if(folderTree) {folderTree.RequireRefresh (['" + GetPathHashCode(srcInfo.FileManagerPath) + "','" + String.Join("','", GetPathHashCodes(destInfo.FileManagerPath)) + "']);}");
 }
        private string OnExecuteCommand(FileManagerItemInfo item, string commandName, string argument)
        {
            ExecuteCommandEventArgs arg = new ExecuteCommandEventArgs(commandName, argument);
            arg.Items.Add(item);
            //arg.Command = fileManagerCommand;

            OnExecuteCommand(arg);

            return arg.ClientScript;
        }
        internal string GetItemThumbnailImage(FileViewItem item, FileManagerItemInfo currentDirectory)
        {
            if (item.IsDirectory)
                return GetFolderLargeImage((DirectoryInfo)item.FileSystemInfo);

            if (IsImage((FileInfo)item.FileSystemInfo))
                return ResolveUrl("~/" + ThumbnailHandler + "?" + HttpUtility.UrlEncode(VirtualPathUtility.AppendTrailingSlash(currentDirectory.VirtualPath) + item.RelativePath));

            return GetFileLargeImage((FileInfo)item.FileSystemInfo);
        }
 void AddFolderTreeRefreshEventReference(StringBuilder sb, FileManagerItemInfo itemInfo)
 {
     sb.AppendLine("var folderTree = window['WFM_' + context.ClientID + '_FolderTree'];");
     sb.AppendLine("if(folderTree) {folderTree.Refresh ('" + GetPathHashCode(itemInfo.FileManagerPath) + "');}");
 }
        private string ProcessOpenCommand(FileManagerItemInfo item)
        {
            var cancelArg = new SelectedItemsActionCancelEventArgs(WebFileManager.SelectedItemsAction.Open);
            cancelArg.SelectedItems.Add(item);
            OnSelectedItemsAction(cancelArg);
            if (cancelArg.Cancel)
                return ClientMessageEventReference(cancelArg.ClientMessage);

            var sb = new StringBuilder();

            if (ClientOpenItemFunction.Length > 0)
            {
                sb.AppendLine("var href = '" + EncodeURIComponent(item.VirtualPath) + "';");
                sb.AppendLine(ClientOpenItemFunction + "(decodeURIComponent(href));");
            }
            else
            {
                sb.AppendLine("var href = '" + EncodeURIComponent(item.VirtualPath) + "';");
                sb.AppendLine(@"window.open(encodeURI(decodeURIComponent(href)).replace(/\+/g, '%2b').replace(/#/g, '%23'),'_blank');");
            }
            return sb.ToString();
        }
        private string ProcessOpenDirectory(FileManagerItemInfo item)
        {
            var cancelArg = new SelectedItemsActionCancelEventArgs(WebFileManager.SelectedItemsAction.Open);
            cancelArg.SelectedItems.Add(item);
            OnSelectedItemsAction(cancelArg);
            if (cancelArg.Cancel)
                return ClientMessageEventReference(cancelArg.ClientMessage);

            var sb = new StringBuilder();
            AddFolderTreeNavigateEventReference(sb, item);
            sb.AppendLine("var dir = '" + EncodeURIComponent(item.FileManagerPath) + "'");
            sb.AppendLine("WebForm_GetElementById(context.ClientID+'_Directory').value = dir;");
            sb.AppendLine("var address = WebForm_GetElementById(context.ClientID+'_Address');");
            sb.AppendLine("if(address) address.value = decodeURIComponent(dir);");
            sb.AppendLine("var searchTerm = WebForm_GetElementById(context.ClientID+'_SearchTerm');");
            sb.AppendLine("if(searchTerm) searchTerm.value = '';");
            sb.AppendLine("window['WFM_' + context.ClientID].ClearSearchBox();");
            sb.AppendLine(ClientRefreshEventReference);
            return sb.ToString();
            
        }
 string ProcessFileViewNavigate(FileManagerItemInfo item)
 {
     if (item.Directory.Exists)
     {
         return ProcessOpenDirectory(item);
     }
     else if (item.File.Exists)
     {
         return ProcessExecuteCommand(new FileManagerItemInfo[] { item }, 0, 0);
     }
     else
         return ClientMessageEventReference(GetResourceString("CannotFindFile", "Cannot find file, Make sure the path is correct."));
 }
        private string ProcessRename(FileManagerItemInfo fileManagerItemInfo, string newName)
        {
            RenameCancelEventArgs cancelArg = new RenameCancelEventArgs();
            cancelArg.FileManagerItem = fileManagerItemInfo;
            cancelArg.NewName = newName;
            OnItemRenaming(cancelArg);
            if (cancelArg.Cancel)
            {
                return ClientMessageEventReference(cancelArg.ClientMessage);
            }

            if (cancelArg.NewName == null || cancelArg.NewName.Length == 0 || cancelArg.NewName.StartsWith("."))
            {
                return ClientMessageEventReference(GetResourceString("MustTypeFileName", "You must type a file name."));
            }

            if (!Validate(cancelArg.NewName))
            {
                return ClientMessageEventReference(GetResourceString("NotAllowedCharacters", "A file name cannot contain any of the following characters: \\/:*?\"<>|"));
            }

            if (fileManagerItemInfo.File.Exists)
            {
                string newFileExt = Path.GetExtension(cancelArg.NewName).ToLower(CultureInfo.InvariantCulture).TrimStart('.');
                if (newFileExt.Length == 0 ||
                    HiddenFilesArray.Contains(newFileExt) ||
                    ProhibitedFilesArray.Contains(newFileExt))
                    cancelArg.NewName += fileManagerItemInfo.File.Extension;
            }

            FileManagerItemInfo renamedItem = ResolveFileManagerItemInfo(fileManagerItemInfo.FileManagerPath.Substring(0, fileManagerItemInfo.FileManagerPath.LastIndexOf('/')) + "/" + cancelArg.NewName);
            if (renamedItem.Directory.Exists || renamedItem.File.Exists)
            {
                string fileExistsMessage = GetResourceString("CannotRenameFile", "Cannot rename file: A file with the name you specified already exists. Specify a different file name.");
                return ClientMessageEventReference(fileExistsMessage);
            }

            bool b = false;
            if (fileManagerItemInfo.Directory.Exists)
            {
                b = true;
                fileManagerItemInfo.Directory.MoveTo(renamedItem.PhysicalPath);
            }
            else if (fileManagerItemInfo.File.Exists)
            {
                fileManagerItemInfo.File.MoveTo(renamedItem.PhysicalPath);
            }

            RenameEventArgs arg = new RenameEventArgs();
            arg.FileManagerItem = renamedItem;
            OnItemRenamed(arg);

            StringBuilder sb = new StringBuilder();
            if (b)
                AddFolderTreeRefreshEventReference(sb, _callbackControl.CurrentDirectory);
            sb.AppendLine(ClientRefreshEventReference);
            return sb.ToString();
        }
        private string ProcessCustomCommand(FileManagerItemInfo item, FileManagerCommand fileManagerCommand)
        {
            if (Page.IsCallback && fileManagerCommand.Method == FileManagerCommandMethod.PostBack)
                return
                    Page.ClientScript.GetPostBackEventReference(this,
                                                                "ExecuteCommand:" +
                                                                EncodeURIComponent(item.FileManagerPath) + ":" +
                                                                fileManagerCommand.CommandName + ":" +
                                                                fileManagerCommand.CommandArgument) + ";";

            return OnExecuteCommand(item, fileManagerCommand.CommandName, fileManagerCommand.CommandArgument);
        }
        private string ProcessSelectedItemsAction(FileManagerItemInfo srcDir, FileManagerItemInfo destDir, FileManagerItemInfo[] items, SelectedItemsAction action)
        {
            if (items.Length == 0)
                return "";

            SelectedItemsActionCancelEventArgs cancelArg = new SelectedItemsActionCancelEventArgs(action);
            foreach (FileManagerItemInfo item in items)
            {
                if (item.Directory.Exists && destDir.Directory.FullName.StartsWith(item.Directory.FullName, StringComparison.InvariantCultureIgnoreCase))
                    return ClientMessageEventReference(GetResourceString("DestinationFolderIsSubfolder", "The destination folder is a subfolder of the source folder."));
                cancelArg.SelectedItems.Add(item);
            }
            cancelArg.DestinationDirectory = destDir;
            OnSelectedItemsAction(cancelArg);
            if (cancelArg.Cancel)
            {
                return ClientMessageEventReference(cancelArg.ClientMessage);
            }

            StringBuilder sb = new StringBuilder();

            switch (action)
            {
                case IZ.WebFileManager.SelectedItemsAction.Delete:
                    if (ProcessSelectedItemsDelete(items))
                        AddFolderTreeRefreshEventReference(sb, srcDir);
                    break;
                case IZ.WebFileManager.SelectedItemsAction.Move:
                    destDir.EnsureDirectoryExists();
                    if (ProcessSelectedItemsMoveTo(destDir, items))
                    {
                        AddFolderTreeRequireRefreshEventReference(sb, srcDir, destDir);
                        AddFolderTreeNavigateEventReference(sb, srcDir);
                    }
                    break;
                case IZ.WebFileManager.SelectedItemsAction.Copy:
                    destDir.EnsureDirectoryExists();
                    if (ProcessSelectedItemsCopyTo(destDir, items))
                    {
                        AddFolderTreeRequireRefreshEventReference(sb, srcDir, destDir);
                        AddFolderTreeNavigateEventReference(sb, srcDir);
                    }
                    break;
            }

            SelectedItemsActionEventArgs args = new SelectedItemsActionEventArgs();
            OnSelectedItemsActionComplete(args);

            sb.AppendLine(ClientRefreshEventReference);
            return sb.ToString();
        }
 private string ProcessDownloadCommand(FileManagerItemInfo item)
 {
     String script = Page.ClientScript.GetPostBackEventReference(this, "Download:" + EncodeURIComponent(item.FileManagerPath)) + ";" +
         "theForm.__EVENTTARGET.value = '';theForm.__EVENTARGUMENT.value = '';";
     return script;
 }
        private bool ProcessSelectedItemsMoveTo(FileManagerItemInfo destDir, FileManagerItemInfo[] items)
        {
            bool b = false;
            foreach (FileManagerItemInfo item in items)
            {
                if (item.Directory.Exists)
                {
                    b = true;
                    DirectoryInfo dir = item.Directory;
                    if (String.Compare(dir.Parent.FullName.TrimEnd(Path.DirectorySeparatorChar), destDir.PhysicalPath.TrimEnd(Path.DirectorySeparatorChar), true) == 0)
                        continue;

                    string newName;
                    if (AllowOverwrite)
                        newName = dir.Name;
                    else
                        newName = GetNotDuplicatedFolderName(destDir, dir.Name);
                    string newPath = Path.Combine(destDir.PhysicalPath, newName);
                    xDirectory.Copy(dir.FullName, newPath, true);
                    dir.Delete(true);
                }
                else if (item.File.Exists)
                {
                    FileInfo file = item.File;
                    if (String.Compare(file.Directory.FullName.TrimEnd(Path.DirectorySeparatorChar), destDir.PhysicalPath.TrimEnd(Path.DirectorySeparatorChar), true) == 0)
                        continue;

                    string newName;
                    if (AllowOverwrite)
                        newName = file.Name;
                    else
                        newName = GetNotDuplicatedFileName(destDir, Path.GetFileNameWithoutExtension(file.FullName), file.Extension);
                    string newPath = Path.Combine(destDir.PhysicalPath, newName);
                    file.CopyTo(newPath, AllowOverwrite);
                    file.Delete();
                }
            }
            return b;
        }
        private string ProcessExecuteCommand(FileManagerItemInfo[] items, int group, int index)
        {
            if (items.Length == 0)
                return "";

            if (group == 0)
            {
                FileManagerItemInfo item = items[0];

                if (item.Directory.Exists)
                    return ProcessFileViewNavigate(item);

                // Default Command
                if (index == -1)
                    return ProcessOpenCommand(item);
                else if (index == -2)
                    return ProcessDownloadCommand(item);

                FileType ft = GetFileType(item.File);
                if (ft == null || ft.Commands.Count <= index)
                    return DownloadOnDoubleClick ? ProcessDownloadCommand(item) : ProcessOpenCommand(item);

                return ProcessCustomCommand(item, ft.Commands[index]);
            }
            else
            {
            }

            return "";
        }
 internal string ProcessFileUpload(FileManagerItemInfo destDir, System.Web.HttpPostedFile uploadedFile)
 {
     string script;
     if (AllowUpload)
         ProcessFileUpload(destDir, uploadedFile, out script);
     else
         script = ClientMessageEventReference(null);
     return script;
 }
 string ProcessFileViewNavigate(FileManagerItemInfo item)
 {
     if (item.Directory.Exists)
     {
         StringBuilder sb = new StringBuilder();
         AddFolderTreeNavigateEventReference(sb, item);
         sb.AppendLine("var dir = '" + EncodeURIComponent(item.FileManagerPath) + "'");
         sb.AppendLine("WebForm_GetElementById(context.ClientID+'_Directory').value = dir;");
         sb.AppendLine("var address = WebForm_GetElementById(context.ClientID+'_Address');");
         sb.AppendLine("if(address) address.value = decodeURIComponent(dir);");
         sb.AppendLine(ClientRefreshEventReference);
         return sb.ToString();
     }
     else if (item.File.Exists)
     {
         return ProcessExecuteCommand(new FileManagerItemInfo[] { item }, 0, 0);
     }
     else
         return ClientMessageEventReference(GetResourceString("CannotFindFile", "Cannot find file, Make sure the path is correct."));
 }
 private static string GetNotDuplicatedFileName(FileManagerItemInfo destDir, string fileNameWithoutExtension, string extension)
 {
     string fileName = fileNameWithoutExtension;
     int i = 1;
     string originNewFileName = fileNameWithoutExtension;
     while (File.Exists(Path.Combine(destDir.PhysicalPath, fileName + extension)))
     {
         fileName = originNewFileName + "(" + i + ")";
         i++;
     }
     return fileName + extension;
 }
        private string ProcessNewDocument(FileManagerItemInfo destDir, NewDocumentTemplate template)
        {
            NewDocumentCancelEventArgs cancelArg = new NewDocumentCancelEventArgs();
            cancelArg.DestinationDirectory = destDir;
            cancelArg.Template = template;
            OnNewDocumentCreating(cancelArg);

            if (cancelArg.Cancel)
            {
                return ClientMessageEventReference(cancelArg.ClientMessage);
            }

            if (template.MasterFileUrl.Length == 0)
                return "";

            FileInfo masterFile = new FileInfo(Page.MapPath(template.MasterFileUrl));
            if (!masterFile.Exists)
                return "";

            string newFileName = template.NewFileName;
            if (newFileName.Length == 0)
                newFileName = template.Name;

            int i = 1;
            string originNewFileName = newFileName;
            while (File.Exists(Path.Combine(destDir.PhysicalPath, newFileName + masterFile.Extension)))
            {
                newFileName = originNewFileName + "(" + i + ")";
                i++;
            }
            newFileName = newFileName + masterFile.Extension;
            FileManagerItemInfo itemInfo = ResolveFileManagerItemInfo(VirtualPathUtility.AppendTrailingSlash(destDir.FileManagerPath) + newFileName);
            FileInfo newDocument = masterFile.CopyTo(itemInfo.PhysicalPath);

            NewDocumentEventArgs arg = new NewDocumentEventArgs();
            arg.Template = template;
            arg.NewDocument = itemInfo;
            OnNewDocumentCreated(arg);

            return ClientRefreshEventReference;
        }
 void AddFolderTreeNavigateEventReference(StringBuilder sb, FileManagerItemInfo itemInfo)
 {
     sb.AppendLine("var folderTree = window['WFM_' + context.ClientID + '_FolderTree'];");
     sb.AppendLine("if(folderTree) {folderTree.Navigate (['" + String.Join("','", GetPathHashCodes(itemInfo.FileManagerPath)) + "'],0);}");
 }
        private string ProcessCommand(FileManagerItemInfo item, FileManagerCommand fileManagerCommand)
        {
            ExecuteCommandEventArgs arg = new ExecuteCommandEventArgs (fileManagerCommand.CommandName, fileManagerCommand.CommandArgument);
            arg.Items.Add (item);
            //arg.Command = fileManagerCommand;

            OnExecuteCommand (arg);

            return arg.ClientScript;
        }