/// <summary>
    /// Updates information displayed on import form.
    /// </summary>
    private void UpdateImportForm()
    {
        // Get info on next file name
        string nextFileName = GetNextFileName();

        if (!String.IsNullOrEmpty(nextFileName))
        {
            string ext = Path.GetExtension(nextFileName);

            if (!MediaLibraryHelper.IsExtensionAllowed(ext.TrimStart('.')))
            {
                plcMessError.AddError(string.Format(GetString("attach.notallowedextension"), ext, MediaLibraryHelper.GetAllowedExtensions(SiteName).TrimEnd(';').Replace(";", ", ")));
                SetFormEnabled(false);
            }
            else
            {
                SetFormEnabled(true);
                txtImportFileDescription.Text = "";
                txtImportFileName.Text        = URLHelper.GetSafeFileName(Path.GetFileNameWithoutExtension(nextFileName), SiteName, false);
                txtImportFileTitle.Text       = Path.GetFileNameWithoutExtension(nextFileName);

                LoadPreview(nextFileName);
            }
        }
    }
    /// <summary>
    /// Handle storing single media file info into the DB.
    /// </summary>
    private void HandleSingleMediaFile()
    {
        string fileName = txtImportFileName.Text.Trim();

        // Check if the filename is in correct format
        if (!ValidationHelper.IsFileName(fileName))
        {
            SetupTexts();

            // Inform user on error
            RaiseOnAction("importerror", GetString("media.rename.wrongformat"));
            return;
        }

        // Get info on current file path
        string currFileName = GetNextFileName();

        if (!String.IsNullOrEmpty(currFileName))
        {
            string ext = Path.GetExtension(currFileName);
            if (!MediaLibraryHelper.IsExtensionAllowed(ext.TrimStart('.')))
            {
                RaiseOnAction("importerror", string.Format(GetString("attach.notallowedextension"), ext, MediaLibraryHelper.GetAllowedExtensions(SiteName).TrimEnd(';').Replace(";", ", ")));
                return;
            }
        }

        try
        {
            // Get info on current file path
            if (!String.IsNullOrEmpty(currFileName))
            {
                string filePath = (string.IsNullOrEmpty(FolderPath)) ? currFileName : DirectoryHelper.CombinePath(FolderPath, currFileName);

                // Get file and library info
                FileInfo currFile = FileInfo.New(MediaFileInfoProvider.GetMediaFilePath(SiteName, LibraryInfo.LibraryFolder, filePath));
                if (currFile != null)
                {
                    // Save media file info on current file
                    RaiseOnSaveRequired(currFile, txtImportFileTitle.Text.Trim(), txtImportFileDescription.Text.Trim(), URLHelper.GetSafeFileName(fileName, SiteName, false), filePath);
                }
            }

            // Update file paths set and store updated version in the ViewState
            int indexOfDel = ImportFilePaths.IndexOf('|');
            ImportFilePaths = (indexOfDel > 0 ? ImportFilePaths.Remove(0, indexOfDel + 1) : String.Empty);

            bool wasLast = String.IsNullOrEmpty(ImportFilePaths);
            if (wasLast)
            {
                FinishImport();
            }
            else
            {
                RaiseOnAction("singlefileimported", null);

                // Increment current file index
                ImportCurrFileIndex++;
            }
        }
        catch (Exception ex)
        {
            SetupTexts();

            // Inform user on error
            RaiseOnAction("importerror", ex.Message);
        }
        finally
        {
            SetupImport();
        }
    }
    /// <summary>
    /// Handles storing of multiple media files at once.
    /// </summary>
    private void HandleMultipleMediaFiles()
    {
        try
        {
            string[] filePaths = ImportFilePaths.Split('|');
            if (filePaths.Length > 0)
            {
                bool   first       = true;
                string description = txtImportFileDescription.Text.Trim();
                string title       = txtImportFileTitle.Text.Trim();
                string name        = txtImportFileName.Text.Trim();

                // Check if the filename is in correct format
                if (!ValidationHelper.IsFileName(name))
                {
                    SetupTexts();

                    // Inform user on error
                    RaiseOnAction("importerror", GetString("media.rename.wrongformat"));
                    return;
                }

                // Go through the files and save one by one
                foreach (string path in filePaths)
                {
                    if (path.Trim() != "")
                    {
                        string filePath = DirectoryHelper.CombinePath(FolderPath, path);
                        string ext      = Path.GetExtension(filePath);
                        if (!MediaLibraryHelper.IsExtensionAllowed(ext.TrimStart('.')))
                        {
                            RaiseOnAction("importerror", string.Format(GetString("attach.notallowedextension"), ext, MediaLibraryHelper.GetAllowedExtensions(SiteName).TrimEnd(';').Replace(";", ", ")));
                            return;
                        }

                        // Get file and library info
                        FileInfo currFile = FileInfo.New(RootFolderPath + DirectoryHelper.CombinePath(LibraryInfo.LibraryFolder, filePath));
                        if (currFile != null)
                        {
                            if (first)
                            {
                                // Save media file info on current file
                                RaiseOnSaveRequired(currFile, title, description, URLHelper.GetSafeFileName(name, SiteName, false), filePath);

                                first = false;
                            }
                            else
                            {
                                string fileName = Path.GetFileNameWithoutExtension(currFile.Name);

                                // Save media file info on current file where title = file name
                                RaiseOnSaveRequired(currFile, fileName, description, URLHelper.GetSafeFileName(fileName, SiteName, false), filePath);
                            }
                        }
                    }
                }

                FinishImport();
            }
            else
            {
                // Inform user on error
                RaiseOnAction("importnofiles", null);
            }
        }
        catch (Exception ex)
        {
            SetupTexts();

            // Inform user on error
            RaiseOnAction("importerror", ex.Message);
        }
        finally
        {
            SetupImport();
        }
    }
    /// <summary>
    /// Save title and description of media file info.
    /// </summary>
    /// <param name="fileName">File name</param>
    /// <param name="title">Title</param>
    /// <param name="description">Description</param>
    private bool metaDataEditor_Save(string fileName, string title, string description)
    {
        bool saved = false;

        if (mediaFileInfo != null)
        {
            try
            {
                if (mediaFileInfo.FileName != fileName)
                {
                    // Get original file path
                    string extension = mediaFileInfo.FileExtension;

                    // New file path
                    string newPath      = DirectoryHelper.CombinePath(Path.GetDirectoryName(mediaFileInfo.FilePath), fileName + extension);
                    string newFullPath  = MediaFileInfoProvider.GetMediaFilePath(mediaFileInfo.FileLibraryID, newPath);
                    string newExtension = Path.GetExtension(newPath);

                    if (!String.IsNullOrEmpty(newExtension))
                    {
                        newExtension = newExtension.TrimStart('.');
                    }

                    if (!MediaLibraryHelper.IsExtensionAllowed(newExtension))
                    {
                        // New extension is not allowed
                        metaDataEditor.ShowError(GetString("dialogs.filesystem.NotAllowedExtension").Replace("%%extensions%%", MediaLibraryHelper.GetAllowedExtensions(SiteName).TrimEnd(';').Replace(";", ", ")));
                        return(false);
                    }

                    // Rename file
                    if (!File.Exists(newFullPath))
                    {
                        MediaFileInfoProvider.MoveMediaFile(SiteName, mediaFileInfo.FileLibraryID, mediaFileInfo.FilePath, newPath);

                        // Move preview file if exists
                        if (MediaLibraryHelper.HasPreview(SiteName, mediaFileInfo.FileLibraryID, mediaFileInfo.FilePath))
                        {
                            MediaLibraryHelper.MoveMediaFilePreview(mediaFileInfo, fileName + extension);
                        }
                    }
                    else
                    {
                        // File already exists.
                        metaDataEditor.ShowError(GetString("img.errors.fileexists"));
                        return(false);
                    }

                    mediaFileInfo.FileName = fileName;

                    string subFolderPath = null;

                    int lastSlash = mediaFileInfo.FilePath.LastIndexOfCSafe('/');
                    if (lastSlash > 0)
                    {
                        subFolderPath = mediaFileInfo.FilePath.Substring(0, lastSlash);
                    }

                    if (!string.IsNullOrEmpty(subFolderPath))
                    {
                        mediaFileInfo.FilePath = String.Format("{0}/{1}{2}", subFolderPath, fileName, extension);
                    }
                    else
                    {
                        mediaFileInfo.FilePath = fileName + extension;
                    }
                }
                mediaFileInfo.FileTitle       = title;
                mediaFileInfo.FileDescription = description;

                // Save new data
                MediaFileInfoProvider.SetMediaFileInfo(mediaFileInfo, false);

                saved = true;
            }
            catch (Exception ex)
            {
                metaDataEditor.ShowError(GetString("metadata.errors.processing"));
                EventLogProvider.LogException("Metadata editor", "SAVE", ex);
            }
        }

        return(saved);
    }
Пример #5
0
    /// <summary>
    /// Displays new file form.
    /// </summary>
    /// <param name="file">File the form is displayed for</param>
    public void DisplayForm(FileInfo file)
    {
        if (file != null)
        {
            string ext = file.Extension;

            if (!MediaLibraryHelper.IsExtensionAllowed(ext.TrimStart('.')))
            {
                String errorMsg = String.Format(GetString("attach.notallowedextension"), ext, MediaLibraryHelper.GetAllowedExtensions(SiteContext.CurrentSiteName).TrimEnd(';').Replace(";", ", "));

                plcMessInfo.ClearLabels();
                plcMessInfo.ShowError(errorMsg);
                SetFormVisible(false);
            }
            else
            {
                // If is not in DB fill new file form and show it
                SetFormVisible(true);
                txtNewDescripotion.Text = "";
                txtNewFileTitle.Text    = Path.GetFileNameWithoutExtension(file.Name);
                txtNewFileName.Text     = URLHelper.GetSafeFileName(Path.GetFileNameWithoutExtension(file.Name), SiteContext.CurrentSiteName, false);
            }
        }
    }
    /// <summary>
    /// Updates information displayed on import form.
    /// </summary>
    private void UpdateImportForm()
    {
        // Get info on next file name
        string nextFileName = GetNextFileName();

        if (!String.IsNullOrEmpty(nextFileName))
        {
            string ext = Path.GetExtension(nextFileName);

            if (!MediaLibraryHelper.IsExtensionAllowed(ext.TrimStart('.')))
            {
                this.lblError.Text    = string.Format(GetString("attach.notallowedextension"), ext, MediaLibraryHelper.GetAllowedExtensions(CMSContext.CurrentSiteName).TrimEnd(';').Replace(";", ", "));
                this.lblError.Visible = true;
                SetFormEnabled(false);
            }
            else
            {
                SetFormEnabled(true);
                this.txtImportFileDescription.Text = "";
                this.txtImportFileName.Text        = URLHelper.GetSafeFileName(Path.GetFileNameWithoutExtension(nextFileName), CMSContext.CurrentSiteName, false);
                this.txtImportFileTitle.Text       = Path.GetFileNameWithoutExtension(nextFileName);

                LoadPreview(nextFileName);
            }

            InitializeImportBreadcrumbs(URLHelper.GetSafeFileName(nextFileName, CMSContext.CurrentSiteName));
        }
    }