예제 #1
0
    /// <summary>
    /// New file event handler.
    /// </summary>
    protected void btnNew_Click(object sender, EventArgs e)
    {
        MediaFileInfo mfi = null;

        try
        {
            string newFileName = txtNewFileName.Text.Trim();
            // Check if the filename is in correct format
            if (!ValidationHelper.IsFileName(newFileName))
            {
                lblErrorNew.Text = GetString("media.rename.wrongformat");
                ErrorOccurred    = true;
            }
            else
            {
                // Check 'File create' permission
                if (MediaLibraryInfoProvider.IsUserAuthorizedPerLibrary(LibraryInfo, "filecreate"))
                {
                    if (LibraryInfo != null)
                    {
                        // Get file and library info
                        if (CurrentFile != null)
                        {
                            if (File.Exists(CurrentFile.FullName))
                            {
                                // Save new file in the DB
                                if (SaveRequired != null)
                                {
                                    mfi = SaveRequired(CurrentFile, txtNewFileTitle.Text.Trim(), txtNewDescripotion.Text.Trim(), URLHelper.GetSafeFileName(newFileName, SiteContext.CurrentSiteName, false), FilePath);
                                    if (mfi != null)
                                    {
                                        mfi.RelatedData = CurrentFile.FullName;
                                    }
                                }
                            }
                            else
                            {
                                lblErrorNew.Text = GetString("media.newfile.notexist");
                                ErrorOccurred    = true;
                            }
                        }
                    }
                }
                else
                {
                    lblErrorNew.Text = MediaLibraryHelper.GetAccessDeniedMessage("filecreate");
                    ErrorOccurred    = true;
                }
            }
        }
        catch (Exception ex)
        {
            ErrorOccurred    = true;
            lblErrorNew.Text = (ex.InnerException != null ? ex.InnerException.Message : ex.Message);
        }

        // Display user with error encountered
        if (ErrorOccurred)
        {
            lblErrorNew.Visible = true;
            RaiseOnAction("error");
        }
        else
        {
            RaiseOnAction("save", mfi);
        }
    }
예제 #2
0
    /// <summary>
    /// Saves modified image data.
    /// </summary>
    /// <param name="name">Image name</param>
    /// <param name="extension">Image extension</param>
    /// <param name="mimetype">Image mimetype</param>
    /// <param name="title">Image title</param>
    /// <param name="description">Image description</param>
    /// <param name="binary">Image binary data</param>
    /// <param name="width">Image width</param>
    /// <param name="height">Image height</param>
    private void SaveImage(string name, string extension, string mimetype, string title, string description, byte[] binary, int width, int height)
    {
        // Process media file
        if (mfi == null)
        {
            mfi = MediaFileInfoProvider.GetMediaFileInfo(mediafileGuid, CurrentSiteName);
        }

        if (mfi != null)
        {
            MediaLibraryInfo mli = MediaLibraryInfoProvider.GetMediaLibraryInfo(mfi.FileLibraryID);
            if (mli != null)
            {
                string path          = Path.GetDirectoryName(DirectoryHelper.CombinePath(MediaLibraryInfoProvider.GetMediaLibraryFolderPath(mli.LibraryID), mfi.FilePath));
                bool   permissionsOK = DirectoryHelper.CheckPermissions(path, false, true, true, true);

                // Check file write permissions
                FileInfo file = FileInfo.New(MediaFileInfoProvider.GetMediaFilePath(mfi.FileLibraryID, mfi.FilePath));
                if (file != null)
                {
                    permissionsOK = permissionsOK && !file.IsReadOnly;
                }

                if (permissionsOK)
                {
                    MediaFileInfo originalMfi = mfi.Clone(true);

                    try
                    {
                        // Ensure object version
                        SynchronizationHelper.EnsureObjectVersion(mfi);

                        if (isPreview && !String.IsNullOrEmpty(PreviewPath))
                        {
                            SiteInfo si = SiteInfoProvider.GetSiteInfo(mfi.FileSiteID);
                            if (si != null)
                            {
                                string previewExt    = (!String.IsNullOrEmpty(extension) && (extension != OldPreviewExt)) ? extension : OldPreviewExt;
                                string previewName   = Path.GetFileNameWithoutExtension(PreviewPath);
                                string previewFolder = Path.EnsureSlashes(DirectoryHelper.CombinePath(Path.GetDirectoryName(mfi.FilePath).TrimEnd('/'), MediaLibraryHelper.GetMediaFileHiddenFolder(si.SiteName)));

                                // Delete old preview files with thumbnails
                                MediaFileInfoProvider.DeleteMediaFilePreview(SiteContext.CurrentSiteName, mli.LibraryID, mfi.FilePath);
                                MediaFileInfoProvider.DeleteMediaFilePreviewThumbnails(mfi);

                                // Save preview file
                                MediaFileInfoProvider.SaveFileToDisk(si.SiteName, mli.LibraryFolder, previewFolder, previewName, previewExt, mfi.FileGUID, binary, false, false);

                                // Log synchronization task
                                SynchronizationHelper.LogObjectChange(mfi, TaskTypeEnum.UpdateObject);
                            }
                        }
                        else
                        {
                            string newExt  = null;
                            string newName = null;

                            if (!String.IsNullOrEmpty(extension))
                            {
                                newExt = extension;
                            }
                            if (!String.IsNullOrEmpty(mimetype))
                            {
                                mfi.FileMimeType = mimetype;
                            }

                            mfi.FileTitle       = title;
                            mfi.FileDescription = description;

                            if (width > 0)
                            {
                                mfi.FileImageWidth = width;
                            }
                            if (height > 0)
                            {
                                mfi.FileImageHeight = height;
                            }
                            if (binary != null)
                            {
                                mfi.FileBinary = binary;
                                mfi.FileSize   = binary.Length;
                            }
                            // Test all parameters to empty values and update new value if available
                            if (!String.IsNullOrEmpty(name))
                            {
                                newName = name;
                            }
                            // If filename changed move preview file and remove all ald thumbnails
                            if ((!String.IsNullOrEmpty(newName) && (mfi.FileName != newName)) || (!String.IsNullOrEmpty(newExt) && (mfi.FileExtension.ToLowerCSafe() != newExt.ToLowerCSafe())))
                            {
                                SiteInfo si = SiteInfoProvider.GetSiteInfo(mfi.FileSiteID);
                                if (si != null)
                                {
                                    string fileName = (newName ?? mfi.FileName);
                                    string fileExt  = (newExt ?? mfi.FileExtension);

                                    string newPath = MediaFileInfoProvider.GetMediaFilePath(mfi.FileLibraryID, DirectoryHelper.CombinePath(Path.GetDirectoryName(mfi.FilePath), fileName) + fileExt);

                                    // Rename file only if file with same name does not exsists
                                    if (!File.Exists(newPath))
                                    {
                                        // Ensure max length of file path
                                        if (newPath.Length < 260)
                                        {
                                            // Remove old thumbnails
                                            MediaFileInfoProvider.DeleteMediaFileThumbnails(mfi);
                                            MediaFileInfoProvider.DeleteMediaFilePreviewThumbnails(mfi);

                                            // Move media file
                                            MediaFileInfoProvider.MoveMediaFile(si.SiteName, mli.LibraryID, mfi.FilePath, DirectoryHelper.CombinePath(Path.GetDirectoryName(mfi.FilePath), fileName) + fileExt, false);

                                            // Set new file name or extension
                                            mfi.FileName      = fileName;
                                            mfi.FileExtension = fileExt;
                                            mfi.FileMimeType  = MimeTypeHelper.GetMimetype(fileExt);

                                            // Ensure new binary
                                            if (binary != null)
                                            {
                                                mfi.FileBinary = binary;
                                                mfi.FileSize   = binary.Length;
                                            }
                                        }
                                        else
                                        {
                                            throw new IOExceptions.PathTooLongException();
                                        }
                                    }
                                    else
                                    {
                                        baseImageEditor.ShowError(GetString("img.errors.fileexists"));
                                        SavingFailed = true;
                                        return;
                                    }
                                }
                            }
                            else
                            {
                                // Remove old thumbnails
                                MediaFileInfoProvider.DeleteMediaFileThumbnails(mfi);

                                // Remove original media file before save
                                string filePath = MediaFileInfoProvider.GetMediaFilePath(mfi.FileLibraryID, mfi.FilePath);
                                if (File.Exists(filePath))
                                {
                                    File.Delete(filePath);
                                }
                            }

                            // Save new data
                            MediaFileInfoProvider.SetMediaFileInfo(mfi, false);
                        }
                    }
                    catch (Exception e)
                    {
                        // Log exception
                        EventLogProvider.LogException("ImageEditor", "Save file", e);

                        baseImageEditor.ShowError(GetString("img.errors.processing"), tooltipText: e.Message);
                        SavingFailed = true;
                        // Save original media file info
                        MediaFileInfoProvider.SetMediaFileInfo(originalMfi, false);
                    }
                }
                else // User hasn't permissions for save file
                {
                    baseImageEditor.ShowError(GetString("img.errors.filesystempermissions"));
                    SavingFailed = true;
                }
            }
        }
    }