/// <summary> /// Initialize labels according to current image type. /// </summary> private void baseImageEditor_InitializeLabels(bool reloadName) { //Initialize strings depending on image type if (baseImageEditor.ImageType == ImageHelper.ImageTypeEnum.MediaFile) { // Initialize media file thumbnail if (isPreview && (previewFile != null) && !String.IsNullOrEmpty(PreviewPath)) { baseImageEditor.TxtFileName.Text = Path.GetFileName(PreviewPath); baseImageEditor.LblExtensionValue.Text = Path.GetExtension(PreviewPath); baseImageEditor.LblImageSizeValue.Text = DataHelper.GetSizeString(previewFile.Length); baseImageEditor.LblWidthValue.Text = baseImageEditor.ImgHelper.ImageWidth.ToString(); baseImageEditor.LblHeightValue.Text = baseImageEditor.ImgHelper.ImageHeight.ToString(); } // Initialize regular media file else if ((mfi != null) && (mfi.FileBinary != null)) { if (!RequestHelper.IsPostBack() || reloadName) { baseImageEditor.TxtFileName.Text = mfi.FileName; } baseImageEditor.LblExtensionValue.Text = mfi.FileExtension; baseImageEditor.LblImageSizeValue.Text = DataHelper.GetSizeString(mfi.FileSize); baseImageEditor.LblWidthValue.Text = mfi.FileImageWidth.ToString(); baseImageEditor.LblHeightValue.Text = mfi.FileImageHeight.ToString(); baseImageEditor.SetTitleAndDescription(mfi.FileTitle, mfi.FileDescription); // Set media file info object baseImageEditor.SetMetaDataInfoObject(mfi); } } }
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); }
/// <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; } } } }
/// <summary> /// Initializes common properties used for processing image. /// </summary> private void baseImageEditor_InitializeProperties() { // Process media file if (baseImageEditor.ImageType == ImageHelper.ImageTypeEnum.MediaFile) { // Get mediafile mfi = MediaFileInfoProvider.GetMediaFileInfo(mediafileGuid, CurrentSiteName); // If file is not null if (mfi != null) { MediaLibraryInfo mli = MediaLibraryInfoProvider.GetMediaLibraryInfo(mfi.FileLibraryID); if ((mli != null) && (MediaLibraryInfoProvider.IsUserAuthorizedPerLibrary(mli, "filemodify"))) { // Load media file thumbnail if (isPreview) { PreviewPath = MediaFileInfoProvider.GetPreviewFilePath(mfi); if (PreviewPath != null) { OldPreviewExt = Path.GetExtension(PreviewPath); try { // Get file contents from file system previewFile = File.ReadAllBytes(PreviewPath); } catch (Exception ex) { EventLogProvider.LogException("ImageEditor", "GetPreviewFile", ex); } if (previewFile != null) { baseImageEditor.ImgHelper = new ImageHelper(previewFile); } else { baseImageEditor.LoadingFailed = true; baseImageEditor.ShowError(GetString("img.errors.loading")); } } else { baseImageEditor.LoadingFailed = true; baseImageEditor.ShowError(GetString("img.errors.loading")); } } // Load media file else { mfi.FileBinary = MediaFileInfoProvider.GetFile(mfi, mli.LibraryFolder, CurrentSiteName); // Ensure metafile binary data if (mfi.FileBinary != null) { baseImageEditor.ImgHelper = new ImageHelper(mfi.FileBinary); } else { baseImageEditor.LoadingFailed = true; baseImageEditor.ShowError(GetString("img.errors.loading")); } } } else { baseImageEditor.LoadingFailed = true; baseImageEditor.ShowError(GetString("img.errors.filemodify")); } } else { baseImageEditor.LoadingFailed = true; baseImageEditor.ShowError(GetString("img.errors.loading")); } } // Check that image is in supported formats if ((!baseImageEditor.LoadingFailed) && (baseImageEditor.ImgHelper.ImageFormatToString() == null)) { baseImageEditor.LoadingFailed = true; baseImageEditor.ShowError(GetString("img.errors.format")); } // Disable editor if loading failed if (baseImageEditor.LoadingFailed) { Enabled = false; } }