示例#1
0
    protected void Page_Load(object sender, EventArgs e)
    {
        mediafileGuid = QueryHelper.GetGuid("mediafileguid", Guid.Empty);
        isPreview     = QueryHelper.GetBoolean("isPreview", false);

        // Get media file information
        if (mfi == null)
        {
            mfi = MediaFileInfoProvider.GetMediaFileInfo(mediafileGuid, CurrentSiteName);
        }

        if (mfi != null)
        {
            // Get media library information
            MediaLibraryInfo mli = MediaLibraryInfoProvider.GetMediaLibraryInfo(mfi.FileLibraryID);

            if (mli != null)
            {
                // Get path to media file folder
                string path = Path.GetDirectoryName(DirectoryHelper.CombinePath(MediaLibraryInfoProvider.GetMediaLibraryFolderPath(mli.LibraryID), mfi.FilePath));

                // Enable control if permissions are sufficient to edit image
                Enabled = DirectoryHelper.CheckPermissions(path, false, true, true, true);

                if (!Enabled)
                {
                    // Set error message
                    baseImageEditor.LblLoadFailed.Visible        = true;
                    baseImageEditor.LblLoadFailed.ResourceString = "img.errors.filesystempermissions";
                }
            }
        }

        // Enable or disable image editor
        baseImageEditor.Enabled = Enabled;

        baseImageEditor.LoadImageType        += new CMSAdminControls_ImageEditor_BaseImageEditor.OnLoadImageType(baseImageEditor_LoadImageType);
        baseImageEditor.InitializeProperties += new CMSAdminControls_ImageEditor_BaseImageEditor.OnInitializeProperties(baseImageEditor_InitializeProperties);
        baseImageEditor.InitializeLabels     += new CMSAdminControls_ImageEditor_BaseImageEditor.OnInitializeLabels(baseImageEditor_InitializeLabels);
        baseImageEditor.SaveImage            += new CMSAdminControls_ImageEditor_BaseImageEditor.OnSaveImage(baseImageEditor_SaveImage);
        baseImageEditor.GetMetaData          += new CMSAdminControls_ImageEditor_BaseImageEditor.OnGetMetaData(baseImageEditor_GetMetaData);
        baseImageEditor.LoadImageUrl         += new CMSAdminControls_ImageEditor_BaseImageEditor.OnLoadImageUrl(baseImageEditor_LoadImageUrl);
    }
示例#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;
                }
            }
        }
    }