Example #1
0
    /// <summary>
    /// Moves document.
    /// </summary>
    private void PerformAction(object parameter)
    {
        if (Action.ToLower() == "copy")
        {
            AddLog(GetString("media.copy.startcopy"));
        }
        else
        {
            AddLog(GetString("media.move.startmove"));
        }

        if (LibraryInfo != null)
        {
            // Library path (used in recursive copy process)
            string libPath = MediaLibraryInfoProvider.GetMediaLibraryFolderPath(CMSContext.CurrentSiteName, LibraryInfo.LibraryFolder);

            // Ensure libPath is in original path type
            libPath = Path.GetFullPath(libPath);

            // Original path on disk from query
            string origPath = Path.GetFullPath(DirectoryHelper.CombinePath(libPath, FolderPath));

            // New path on disk
            string newPath = null;

            // Original path in DB
            string origDBPath = MediaLibraryHelper.EnsurePath(FolderPath);

            // New path in DB
            string newDBPath = null;

            AddLog(NewPath);

            // Check if requested folder is in library root folder
            if (!origPath.StartsWith(libPath, StringComparison.CurrentCultureIgnoreCase))
            {
                CurrentError = GetString("media.folder.nolibrary");
                AddLog(CurrentError);
                return;
            }

            string origFolderName = Path.GetFileName(origPath);

            if ((String.IsNullOrEmpty(Files) && !mAllFiles) && string.IsNullOrEmpty(origFolderName))
            {
                NewPath = NewPath + "\\" + LibraryInfo.LibraryFolder;
                NewPath = NewPath.Trim('\\');
            }
            newPath = NewPath;

            // Process current folder copy/move action
            if (String.IsNullOrEmpty(Files) && !AllFiles)
            {
                newPath = newPath.TrimEnd('\\') + '\\' + origFolderName;
                newPath = newPath.Trim('\\');

                // Check if moving into same folder
                if ((Action.ToLower() == "move") && (newPath == FolderPath))
                {
                    CurrentError = GetString("media.move.foldermove");
                    AddLog(CurrentError);
                    return;
                }

                // Error if moving folder into itself
                string newRootPath           = Path.GetDirectoryName(newPath).Trim();
                string newSubRootFolder      = Path.GetFileName(newPath).ToLower().Trim();
                string originalSubRootFolder = Path.GetFileName(FolderPath).ToLower().Trim();
                if (String.IsNullOrEmpty(Files) && (Action.ToLower() == "move") && newPath.StartsWith(DirectoryHelper.EnsurePathBackSlash(FolderPath)) &&
                    (originalSubRootFolder == newSubRootFolder) && (newRootPath == FolderPath))
                {
                    CurrentError = GetString("media.move.movetoitself");
                    AddLog(CurrentError);
                    return;
                }

                // Get unique path for copy or move
                string path = Path.GetFullPath(DirectoryHelper.CombinePath(libPath, newPath));
                path    = MediaLibraryHelper.EnsureUniqueDirectory(path);
                newPath = path.Remove(0, (libPath.Length + 1));

                // Get new DB path
                newDBPath = MediaLibraryHelper.EnsurePath(newPath.Replace(DirectoryHelper.EnsurePathBackSlash(libPath), ""));
            }
            else
            {
                origDBPath = MediaLibraryHelper.EnsurePath(FolderPath);
                newDBPath  = MediaLibraryHelper.EnsurePath(newPath.Replace(libPath, "")).Trim('/');
            }

            // Error if moving folder into its subfolder
            if ((String.IsNullOrEmpty(Files) && !AllFiles) && (Action.ToLower() == "move") && newPath.StartsWith(DirectoryHelper.EnsurePathBackSlash(FolderPath)))
            {
                CurrentError = GetString("media.move.parenttochild");
                AddLog(CurrentError);
                return;
            }

            // Error if moving files into same directory
            if ((!String.IsNullOrEmpty(Files) || AllFiles) && (Action.ToLower() == "move") && (newPath.TrimEnd('\\') == FolderPath.TrimEnd('\\')))
            {
                CurrentError = GetString("media.move.fileserror");
                AddLog(CurrentError);
                return;
            }

            NewPath       = newPath;
            refreshScript = "if ((typeof(window.top.opener) != 'undefined') && (typeof(window.top.opener.RefreshLibrary) != 'undefined')) {window.top.opener.RefreshLibrary(" + ScriptHelper.GetString(NewPath.Replace('\\', '|')) + ");} else if ((typeof(window.top.wopener) != 'undefined') && (typeof(window.top.wopener.RefreshLibrary) != 'undefined')) { window.top.wopener.RefreshLibrary(" + ScriptHelper.GetString(NewPath.Replace('\\', '|')) + "); } window.top.close();";

            // If mFiles is empty handle directory copy/move
            if (String.IsNullOrEmpty(Files) && !mAllFiles)
            {
                try
                {
                    switch (Action.ToLower())
                    {
                    case "move":
                        MediaLibraryInfoProvider.MoveMediaLibraryFolder(CMSContext.CurrentSiteName, MediaLibraryID, origDBPath, newDBPath, false);
                        break;

                    case "copy":
                        MediaLibraryInfoProvider.CopyMediaLibraryFolder(CMSContext.CurrentSiteName, MediaLibraryID, origDBPath, newDBPath, false, CurrentUser.UserID);
                        break;
                    }
                }
                catch (UnauthorizedAccessException ex)
                {
                    CurrentError = GetString("general.erroroccurred") + " " + GetString("media.security.accessdenied");
                    EventLogProvider ev = new EventLogProvider();
                    ev.LogEvent("MediaFolder", this.Action, ex);
                    AddLog(CurrentError);
                    return;
                }
                catch (ThreadAbortException ex)
                {
                    string state = ValidationHelper.GetString(ex.ExceptionState, string.Empty);
                    if (state == CMSThread.ABORT_REASON_STOP)
                    {
                        // When canceled
                        CurrentInfo = GetString("general.actioncanceled");
                        AddLog(CurrentInfo);
                    }
                    else
                    {
                        // Log error
                        CurrentError = GetString("general.erroroccurred") + " " + ex.Message;
                        EventLogProvider ev = new EventLogProvider();
                        ev.LogEvent("MediaFolder", this.Action, ex);
                        AddLog(CurrentError);
                        return;
                    }
                }
                catch (Exception ex)
                {
                    CurrentError = GetString("general.erroroccurred") + " " + ex.Message;
                    EventLogProvider ev = new EventLogProvider();
                    ev.LogEvent("MediaFolder", this.Action, ex);
                    AddLog(CurrentError);
                    return;
                }
            }
            else
            {
                string origDBFilePath = null;
                string newDBFilePath  = null;

                if (!mAllFiles)
                {
                    try
                    {
                        string[] files = Files.Split('|');
                        foreach (string filename in files)
                        {
                            origDBFilePath = (string.IsNullOrEmpty(origDBPath)) ? filename : origDBPath + "/" + filename;
                            newDBFilePath  = (string.IsNullOrEmpty(newDBPath)) ? filename : newDBPath + "/" + filename;
                            AddLog(filename);
                            CopyMove(origDBFilePath, newDBFilePath);
                        }
                    }
                    catch (UnauthorizedAccessException ex)
                    {
                        CurrentError = GetString("general.erroroccurred") + " " + ResHelper.GetString("media.security.accessdenied");
                        EventLogProvider ev = new EventLogProvider();
                        ev.LogEvent("MediaFile", this.Action, ex);
                        AddLog(CurrentError);
                        return;
                    }
                    catch (ThreadAbortException ex)
                    {
                        string state = ValidationHelper.GetString(ex.ExceptionState, string.Empty);
                        if (state == CMSThread.ABORT_REASON_STOP)
                        {
                            // When canceled
                            CurrentInfo = GetString("general.actioncanceled");
                            AddLog(CurrentInfo);
                        }
                        else
                        {
                            // Log error
                            CurrentError = GetString("general.erroroccurred") + " " + ex.Message;
                            EventLogProvider ev = new EventLogProvider();
                            ev.LogEvent("MediaFile", this.Action, ex);
                            AddLog(CurrentError);
                            return;
                        }
                    }
                    catch (Exception ex)
                    {
                        CurrentError = GetString("general.erroroccurred") + " " + ex.Message;
                        EventLogProvider ev = new EventLogProvider();
                        ev.LogEvent("MediaFile", this.Action, ex);
                        AddLog(CurrentError);
                        return;
                    }
                }
                else
                {
                    HttpContext context = (parameter as HttpContext);
                    if (context != null)
                    {
                        HttpContext.Current = context;

                        DataSet files = GetFileSystemDataSource();
                        if (!DataHelper.IsEmpty(files))
                        {
                            foreach (DataRow file in files.Tables[0].Rows)
                            {
                                string fileName = ValidationHelper.GetString(file["FileName"], "");

                                AddLog(fileName);

                                origDBFilePath = (string.IsNullOrEmpty(origDBPath)) ? fileName : origDBPath + "/" + fileName;
                                newDBFilePath  = (string.IsNullOrEmpty(newDBPath)) ? fileName : newDBPath + "/" + fileName;

                                // Clear current httpcontext for CopyMove action in threat
                                HttpContext.Current = null;

                                try
                                {
                                    CopyMove(origDBFilePath, newDBFilePath);
                                }
                                catch (UnauthorizedAccessException ex)
                                {
                                    CurrentError = GetString("general.erroroccurred") + " " + ResHelper.GetString("media.security.accessdenied");
                                    EventLogProvider ev = new EventLogProvider();
                                    ev.LogEvent("MediaFile", this.Action, ex);
                                    AddLog(CurrentError);
                                    return;
                                }
                                catch (ThreadAbortException ex)
                                {
                                    string state = ValidationHelper.GetString(ex.ExceptionState, string.Empty);
                                    if (state == CMSThread.ABORT_REASON_STOP)
                                    {
                                        // When canceled
                                        CurrentInfo = GetString("general.actioncanceled");
                                        AddLog(CurrentInfo);
                                    }
                                    else
                                    {
                                        // Log error
                                        CurrentError = GetString("general.erroroccurred") + " " + ex.Message;
                                        EventLogProvider ev = new EventLogProvider();
                                        ev.LogEvent("MediaFile", this.Action, ex);
                                        AddLog(CurrentError);
                                        return;
                                    }
                                }
                                catch (Exception ex)
                                {
                                    CurrentError = GetString("general.erroroccurred") + " " + ex.Message;
                                    EventLogProvider ev = new EventLogProvider();
                                    ev.LogEvent("MediaFile", this.Action, ex);
                                    AddLog(CurrentError);
                                    return;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    /// <summary>
    /// Moves document.
    /// </summary>
    private void PerformAction(object parameter)
    {
        AddLog(GetString(CopyMoveAction.ToLowerCSafe() == "copy" ? "media.copy.startcopy" : "media.move.startmove"));

        if (LibraryInfo != null)
        {
            // Library path (used in recursive copy process)
            string libPath = MediaLibraryInfoProvider.GetMediaLibraryFolderPath(SiteContext.CurrentSiteName, LibraryInfo.LibraryFolder);

            // Ensure libPath is in original path type
            libPath = Path.GetFullPath(libPath);

            // Original path on disk from query
            string origPath = Path.GetFullPath(DirectoryHelper.CombinePath(libPath, FolderPath));

            // Original path in DB
            string origDBPath = Path.EnsureForwardSlashes(FolderPath);

            // New path in DB
            string newDBPath;

            AddLog(NewPath);

            // Check if requested folder is in library root folder
            if (!origPath.StartsWithCSafe(libPath, true))
            {
                CurrentError = GetString("media.folder.nolibrary");
                AddLog(CurrentError);
                return;
            }

            string origFolderName = Path.GetFileName(origPath);

            if ((String.IsNullOrEmpty(Files) && !mAllFiles) && string.IsNullOrEmpty(origFolderName))
            {
                NewPath = NewPath + "\\" + LibraryInfo.LibraryFolder;
                NewPath = NewPath.Trim('\\');
            }

            // New path on disk
            string newPath = NewPath;

            // Process current folder copy/move action
            if (String.IsNullOrEmpty(Files) && !AllFiles)
            {
                newPath = Path.EnsureEndSlash(newPath) + origFolderName;
                newPath = newPath.Trim('\\');

                // Check if moving into same folder
                if ((CopyMoveAction.ToLowerCSafe() == "move") && (newPath == FolderPath))
                {
                    CurrentError = GetString("media.move.foldermove");
                    AddLog(CurrentError);
                    return;
                }

                // Error if moving folder into itself
                string newRootPath           = Path.GetDirectoryName(newPath).Trim();
                string newSubRootFolder      = Path.GetFileName(newPath).ToLowerCSafe().Trim();
                string originalSubRootFolder = Path.GetFileName(FolderPath).ToLowerCSafe().Trim();
                if (String.IsNullOrEmpty(Files) && (CopyMoveAction.ToLowerCSafe() == "move") && newPath.StartsWithCSafe(Path.EnsureEndSlash(FolderPath)) &&
                    (originalSubRootFolder == newSubRootFolder) && (newRootPath == FolderPath))
                {
                    CurrentError = GetString("media.move.movetoitself");
                    AddLog(CurrentError);
                    return;
                }

                try
                {
                    // Get unique path for copy or move
                    string path = Path.GetFullPath(DirectoryHelper.CombinePath(libPath, newPath));
                    path    = MediaLibraryHelper.EnsureUniqueDirectory(path);
                    newPath = path.Remove(0, (libPath.Length + 1));

                    // Get new DB path
                    newDBPath = Path.EnsureForwardSlashes(newPath.Replace(Path.EnsureEndSlash(libPath), ""));
                }
                catch (Exception ex)
                {
                    CurrentError = GetString("general.erroroccurred") + " " + ex.Message;
                    Service.Resolve <IEventLogService>().LogException("MediaFolder", CopyMoveAction, ex);
                    AddLog(CurrentError);
                    return;
                }
            }
            else
            {
                origDBPath = Path.EnsureForwardSlashes(FolderPath);
                newDBPath  = Path.EnsureForwardSlashes(newPath.Replace(libPath, "")).Trim('/');
            }

            // Error if moving folder into its subfolder
            if ((String.IsNullOrEmpty(Files) && !AllFiles) && (CopyMoveAction.ToLowerCSafe() == "move") && newPath.StartsWithCSafe(Path.EnsureEndSlash(FolderPath)))
            {
                CurrentError = GetString("media.move.parenttochild");
                AddLog(CurrentError);
                return;
            }

            // Error if moving files into same directory
            if ((!String.IsNullOrEmpty(Files) || AllFiles) && (CopyMoveAction.ToLowerCSafe() == "move") && (newPath.TrimEnd('\\') == FolderPath.TrimEnd('\\')))
            {
                CurrentError = GetString("media.move.fileserror");
                AddLog(CurrentError);
                return;
            }

            NewPath      = newPath;
            AsyncNewPath = newPath;

            // If mFiles is empty handle directory copy/move
            if (String.IsNullOrEmpty(Files) && !mAllFiles)
            {
                try
                {
                    switch (CopyMoveAction.ToLowerCSafe())
                    {
                    case "move":
                        MediaLibraryInfoProvider.MoveMediaLibraryFolder(SiteContext.CurrentSiteName, MediaLibraryID, origDBPath, newDBPath);
                        break;

                    case "copy":
                        MediaLibraryInfoProvider.CopyMediaLibraryFolder(SiteContext.CurrentSiteName, MediaLibraryID, origDBPath, newDBPath, CurrentUser.UserID);
                        break;
                    }
                }
                catch (UnauthorizedAccessException ex)
                {
                    CurrentError = GetString("general.erroroccurred") + " " + GetString("media.security.accessdenied");
                    Service.Resolve <IEventLogService>().LogException("MediaFolder", CopyMoveAction, ex);
                    AddLog(CurrentError);
                }
                catch (ThreadAbortException ex)
                {
                    if (CMSThread.Stopped(ex))
                    {
                        // When canceled
                        CurrentInfo = GetString("general.actioncanceled");
                        AddLog(CurrentInfo);
                    }
                    else
                    {
                        // Log error
                        CurrentError = GetString("general.erroroccurred") + " " + ex.Message;
                        Service.Resolve <IEventLogService>().LogException("MediaFolder", CopyMoveAction, ex);
                        AddLog(CurrentError);
                    }
                }
                catch (Exception ex)
                {
                    CurrentError = GetString("general.erroroccurred") + " " + ex.Message;
                    Service.Resolve <IEventLogService>().LogException("MediaFolder", CopyMoveAction, ex);
                    AddLog(CurrentError);
                }
            }
            else
            {
                string origDBFilePath;
                string newDBFilePath;

                if (!mAllFiles)
                {
                    try
                    {
                        string[] files = Files.Split('|');
                        foreach (string filename in files)
                        {
                            origDBFilePath = (string.IsNullOrEmpty(origDBPath)) ? filename : origDBPath + "/" + filename;
                            newDBFilePath  = (string.IsNullOrEmpty(newDBPath)) ? filename : newDBPath + "/" + filename;
                            AddLog(filename);
                            CopyMove(origDBFilePath, newDBFilePath);
                        }
                    }
                    catch (UnauthorizedAccessException ex)
                    {
                        CurrentError = GetString("general.erroroccurred") + " " + GetString("media.security.accessdenied");
                        Service.Resolve <IEventLogService>().LogException("MediaFile", CopyMoveAction, ex);
                        AddLog(CurrentError);
                    }
                    catch (ThreadAbortException ex)
                    {
                        if (CMSThread.Stopped(ex))
                        {
                            // When canceled
                            CurrentInfo = GetString("general.actioncanceled");
                            AddLog(CurrentInfo);
                        }
                        else
                        {
                            // Log error
                            CurrentError = GetString("general.erroroccurred") + " " + ex.Message;
                            Service.Resolve <IEventLogService>().LogException("MediaFile", CopyMoveAction, ex);
                            AddLog(CurrentError);
                        }
                    }
                    catch (Exception ex)
                    {
                        CurrentError = GetString("general.erroroccurred") + " " + ex.Message;
                        Service.Resolve <IEventLogService>().LogException("MediaFile", CopyMoveAction, ex);
                        AddLog(CurrentError);
                    }
                }
                else
                {
                    var fileNames = GetFileNames().ToList();
                    if (!fileNames.Any())
                    {
                        return;
                    }

                    foreach (string fileName in fileNames)
                    {
                        AddLog(fileName);

                        origDBFilePath = (string.IsNullOrEmpty(origDBPath)) ? fileName : origDBPath + "/" + fileName;
                        newDBFilePath  = (string.IsNullOrEmpty(newDBPath)) ? fileName : newDBPath + "/" + fileName;

                        try
                        {
                            CopyMove(origDBFilePath, newDBFilePath);
                        }
                        catch (UnauthorizedAccessException ex)
                        {
                            CurrentError = GetString("general.erroroccurred") + " " + GetString("media.security.accessdenied");
                            Service.Resolve <IEventLogService>().LogException("MediaFile", CopyMoveAction, ex);
                            AddLog(CurrentError);
                            return;
                        }
                        catch (ThreadAbortException ex)
                        {
                            if (CMSThread.Stopped(ex))
                            {
                                // When canceled
                                CurrentInfo = GetString("general.actioncanceled");
                                AddLog(CurrentInfo);
                            }
                            else
                            {
                                // Log error
                                CurrentError = GetString("general.erroroccurred") + " " + ex.Message;
                                Service.Resolve <IEventLogService>().LogException("MediaFile", CopyMoveAction, ex);
                                AddLog(CurrentError);
                                return;
                            }
                        }
                        catch (Exception ex)
                        {
                            CurrentError = GetString("general.erroroccurred") + " " + ex.Message;
                            Service.Resolve <IEventLogService>().LogException("MediaFile", CopyMoveAction, ex);
                            AddLog(CurrentError);
                            return;
                        }
                    }
                }
            }
        }
    }