/// <summary>
        /// 
        /// </summary>
        /// <param name="man"></param>
        /// <param name="file"></param>
        /// <param name="type"></param>
        /// <param name="info"></param>
        /// <returns></returns>
        public override bool OnCustomInfo(ManagerEngine man, IFile file, string type, Hashtable info)
        {
            string ext = PathUtils.GetExtension(file.Name).ToLower();

            info["thumbnail"] = false;

            switch (ext) {
                case "gif":
                case "jpeg":
                case "jpg":
                case "png":
                    MediaInfo imageSize = new MediaInfo(file.AbsolutePath);
                    ManagerConfig config = file.Config;
                    int thumbWidth, thumbHeight;
                    double scale;

                    // Constrain proportions
                    scale = Math.Min(config.GetInt("thumbnail.width", 90) / (double) imageSize.Width, config.GetInt("thumbnail.height", 90) / (double) imageSize.Height);

                    if (config.Get("thumbnail.scale_mode", "percentage") == "percentage") {
                        thumbWidth = scale > 1 ? imageSize.Width : (int) Math.Floor(imageSize.Width * scale);
                        thumbHeight = scale > 1 ? imageSize.Height : (int) Math.Floor(imageSize.Height * scale);
                    } else {
                        thumbWidth = config.GetInt("thumbnail.width", 90);
                        thumbHeight = config.GetInt("thumbnail.height", 90);
                    }

                    info["width"] = imageSize.Width;
                    info["height"] = imageSize.Height;
                    info["editable"] = true;
                    info["twidth"] = thumbWidth;
                    info["theight"] = thumbHeight;
                    info["thumbnail"] = true;

                    // Get thumbnail URL
                    if (type == "insert") {
                        IFile thumbFile = man.GetFile(file.Parent + "/" + config.Get("thumbnail.folder", "mcith") + "/" + config.Get("thumbnail.prefix", "mcith") + file.Name);

                        if (thumbFile.Exists)
                            info["thumbnail_url"] = man.ConvertPathToURI(thumbFile.AbsolutePath);
                    }

                    break;
            }

            return true;
        }
        private IFile MakeThumb(ManagerEngine man, IFile file)
        {
            ManagerConfig config = file.Config;
            IFile thumbFile;
            int thumbWidth, thumbHeight;
            int configWidth, configHeight;
            double scale;
            MediaInfo thumbSize = new MediaInfo();

            // Is not enabled
            if (!config.GetBool("thumbnail.enabled", true))
                return file;

            // Setup thumbnail path
            IFile thumbDir = man.GetFile(file.Parent, config.Get("thumbnail.folder", "mcith"));
            if (!thumbDir.Exists)
                thumbDir.MkDir();

            configWidth = config.GetInt("thumbnail.width", 90);
            configHeight = config.GetInt("thumbnail.height", 90);

            // Make thumbnail
            thumbFile = man.GetFile(thumbDir.AbsolutePath, config.Get("thumbnail.prefix", "mcith") + file.Name);
            MediaInfo imageSize = new MediaInfo(file.AbsolutePath);

            // Need to scale?
            if (imageSize.Width < configWidth && imageSize.Height < configHeight)
                return file;

            // To large
            if (imageSize.Width > config.GetInt("thumbnail.max_width", 65535) || imageSize.Height > config.GetInt("thumbnail.max_height", 65535))
                return file;

            // Constrain proportions
            scale = Math.Min(configWidth / (double) imageSize.Width, configHeight / (double) imageSize.Height);

            if (config.Get("thumbnail.scale_mode", "percentage") == "percentage") {
                thumbWidth = scale > 1 ? imageSize.Width : (int) Math.Floor(imageSize.Width * scale);
                thumbHeight = scale > 1 ? imageSize.Height : (int) Math.Floor(imageSize.Height * scale);
            } else {
                thumbWidth = config.GetInt("thumbnail.width", 90);
                thumbHeight = config.GetInt("thumbnail.height", 90);
            }

            if (thumbFile.Exists)
                thumbSize = new MediaInfo(thumbFile.AbsolutePath);

            if (!thumbFile.Exists || thumbSize.Width != thumbWidth || thumbSize.Height != thumbHeight || file.LastModified != thumbFile.LastModified)
                ImageUtils.MakeThumbnail(file.AbsolutePath, thumbFile.AbsolutePath, thumbWidth, thumbHeight, file.LastModified, config.GetInt("thumbnail.jpeg_quality", 75));

            return thumbFile;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="man"></param>
        /// <param name="action"></param>
        /// <param name="file1"></param>
        /// <param name="file2"></param>
        /// <returns></returns>
        public override bool OnFileAction(ManagerEngine man, FileAction action, IFile file1, IFile file2)
        {
            IFile thumbnailFolder, thumbnail;
            ManagerConfig config;

            switch (action) {
                case FileAction.Add:
                    config = file1.Config;

                    if (config.Get("upload.format") != null)
                        ImageUtils.FormatImage(file1.AbsolutePath, config.Get("upload.format"), config.GetInt("upload.autoresize_jpeg_quality", 90));

                    if (config.GetBool("upload.create_thumbnail", true))
                        thumbnail = this.MakeThumb(man, file1);

                    if (config.GetBool("upload.autoresize", false)) {
                        string ext = PathUtils.GetExtension(file1.Name).ToLower();
                        int newWidth, newHeight, configWidth, configHeight;
                        double scale;

                        // Validate format
                        if (ext != "gif" && ext != "jpeg" && ext != "jpg" && ext != "png")
                            return true;

                        MediaInfo imageInfo = new MediaInfo(file1.AbsolutePath);

                        configWidth = config.GetInt("upload.max_width", 1024);
                        configHeight = config.GetInt("upload.max_height", 768);

                        // Needs scaling?
                        if (imageInfo.Width > configWidth || imageInfo.Height > configHeight) {
                            scale = Math.Min(configWidth / (double) imageInfo.Width, configHeight / (double) imageInfo.Height);
                            newWidth = scale > 1 ? imageInfo.Width : (int) Math.Floor(imageInfo.Width * scale);
                            newHeight = scale > 1 ? imageInfo.Height : (int) Math.Floor(imageInfo.Height * scale);

                            ImageUtils.ResizeImage(file1.AbsolutePath, file1.AbsolutePath, newWidth, newHeight, config.GetInt("upload.autoresize_jpeg_quality", 90));
                        }
                    }

                    break;

                case FileAction.Delete:
                    config = file1.Config;

                    if (config.GetBool("thumbnail.delete", true)) {
                        thumbnailFolder = man.GetFile(file1.Parent, config["thumbnail.folder"]);
                        thumbnail = man.GetFile(thumbnailFolder.AbsolutePath, config.Get("thumbnail.prefix", "mcith") + file1.Name);

                        if (thumbnail.Exists)
                            thumbnail.Delete();

                        // Delete empty thumbnail folder
                        if (thumbnailFolder.Exists && thumbnailFolder.ListFiles().Length == 0)
                            thumbnailFolder.Delete();
                    }
                    break;
            }

            return true;
        }
        private ResultSet GetMediaInfo(ManagerEngine man, Hashtable input)
        {
            ResultSet rs = new ResultSet(new string[]{"name", "path", "url", "size", "type", "created", "modified", "width", "height", "attribs", "next", "prev", "custom"});
            BasicFileFilter fileFilter;
            IFile file, parent;
            IFile[] files;
            string prev, next, attribs, url, ext;
            int width, height;
            bool match;
            Hashtable customInfo = new Hashtable();
            ManagerConfig config;

            if (input["url"] != null)
                input["path"] = man.ConvertURIToPath(new Uri((string) input["url"]).AbsolutePath);

            file = man.GetFile(man.DecryptPath((string) input["path"]));
            config = file.Config;
            parent = file.ParentFile;

            if (parent.IsDirectory) {
                // Setup file filter
                fileFilter = new BasicFileFilter();
                //fileFilter->setDebugMode(true);
                fileFilter.IncludeDirectoryPattern = config["filesystem.include_directory_pattern"];
                fileFilter.ExcludeDirectoryPattern = config["filesystem.exclude_directory_pattern"];
                fileFilter.IncludeFilePattern = config["filesystem.include_file_pattern"];
                fileFilter.ExcludeFilePattern = config["filesystem.exclude_file_pattern"];
                fileFilter.IncludeExtensions = config["filesystem.extensions"];
                fileFilter.OnlyFiles = true;

                // List files
                files = parent.ListFilesFiltered(fileFilter);
            } else
                throw new ManagerException("{#error.file_not_exists}");

            match = false;
            prev = "";
            next = "";

            // Find next and prev
            foreach (IFile curFile in files) {
                if (curFile.AbsolutePath == file.AbsolutePath) {
                    match = true;
                    continue;
                } else if (!match)
                    prev = curFile.AbsolutePath;

                if (match) {
                    next = curFile.AbsolutePath;
                    break;
                }
            }

            ext = PathUtils.GetExtension(file.Name).ToLower();

            // Input default size?
            MediaInfo size = new MediaInfo(file.AbsolutePath);
            width = size.Width != -1 ? size.Width : 425;
            height = size.Height != -1 ? size.Height : 350;

            // Get custom info
            man.DispatchEvent(EventType.CustomInfo, file, "info", customInfo);

            attribs = (file.CanRead && config.GetBool("filesystem.readable", true) ? "R" : "-") + (file.CanWrite && config.GetBool("filesystem.writable", true) ? "W" : "-");
            url = PathUtils.RemoveTrailingSlash(config["preview.urlprefix"]) + man.ConvertPathToURI(file.AbsolutePath);
            rs.Add(file.Name,
                       man.EncryptPath(file.AbsolutePath),
                       url,
                       file.Length,
                       ext,
                       StringUtils.GetDate(file.CreationDate, config.Get("filesystem.datefmt", "yyyy-MM-dd HH:mm")),
                       StringUtils.GetDate(file.LastModified, config.Get("filesystem.datefmt", "yyyy-MM-dd HH:mm")),
                       width,
                       height,
                       attribs,
                       man.EncryptPath(next),
                       man.EncryptPath(prev),
                       customInfo);

            return rs;
        }
Exemple #5
0
        /// <summary>
        ///  Delete formats for the specified image based.
        ///
        ///  Format parameters:
        ///  %f - Filename.
        ///  %e - Extension.
        ///  %w - Image width.
        ///  %h - Image height.
        ///  %tw - Target width.
        ///  %th - Target height.
        ///  %ow - Original width.
        ///  %oh - Original height.
        ///
        ///  Examples:
        ///  320x240|gif=%f_%w_%h.gif,320x240=%f_%w_%h.%e
        /// </summary>
        /// <param name="path">File name to format.</param>
        /// <param name="format">Format string to process.</param>
        public static void DeleteFormatImages(string path, string format)
        {
            int       targetWidth, targetHeight, newWidth, newHeight;
            string    fileName, extension, outPath;
            MediaInfo info;
            double    scale;

            if (!File.Exists(path))
            {
                return;
            }

            info = new MediaInfo(path);

            foreach (string chunk in format.Split(new char[] { ',' }))
            {
                Match chunkMatch = Regex.Match(chunk, @"\s?([^=]+)\s?=(.+)\s?");

                if (chunkMatch.Success)
                {
                    fileName     = Path.GetFileNameWithoutExtension(path);
                    extension    = Path.GetExtension(path).Replace(".", "");
                    targetWidth  = newWidth = info.Width;
                    targetHeight = newHeight = info.Height;

                    // Parse all items
                    foreach (string item in chunkMatch.Groups[1].Value.Split(new char[] { '|' }))
                    {
                        switch (item.ToLower())
                        {
                        case "gif":
                        case "jpeg":
                        case "jpg":
                        case "png":
                        case "bmp":
                            extension = item;
                            break;

                        default:
                            Match match = Regex.Match(item, @"\s?([0-9\*]+)\s?x([0-9\*]+)\s?");

                            if (match.Success)
                            {
                                try {
                                    targetWidth = Convert.ToInt32(match.Groups[1].Value);
                                } catch {
                                    // Ignore
                                }

                                try {
                                    targetHeight = Convert.ToInt32(match.Groups[2].Value);
                                } catch {
                                    // Ignore
                                }

                                try {
                                    if (match.Groups[1].Value == "*")
                                    {
                                        // Width is omitted
                                        targetWidth = (int)Math.Floor((double)info.Width / (info.Height / targetHeight));
                                    }

                                    if (match.Groups[2].Value == "*")
                                    {
                                        // Height is omitted
                                        targetHeight = (int)Math.Floor((double)info.Height / (info.Width / targetWidth));
                                    }
                                } catch {
                                    // Ignore
                                }
                            }

                            break;
                        }
                    }

                    // Scale it
                    if (targetWidth != info.Width || targetHeight != info.Height)
                    {
                        scale     = Math.Min(targetWidth / (double)info.Width, targetHeight / (double)info.Height);
                        newWidth  = scale > 1 ? info.Width : (int)Math.Floor((double)info.Width * scale);
                        newHeight = scale > 1 ? info.Height : (int)Math.Floor((double)info.Height * scale);
                    }

                    // Build output path
                    outPath = chunkMatch.Groups[2].Value;
                    outPath = outPath.Replace("%f", fileName);
                    outPath = outPath.Replace("%e", extension);
                    outPath = outPath.Replace("%ow", "" + info.Width);
                    outPath = outPath.Replace("%oh", "" + info.Height);
                    outPath = outPath.Replace("%tw", "" + targetWidth);
                    outPath = outPath.Replace("%th", "" + targetHeight);
                    outPath = outPath.Replace("%w", "" + newWidth);
                    outPath = outPath.Replace("%h", "" + newHeight);
                    outPath = PathUtils.AddTrailingSlash(PathUtils.ToUnixPath(Path.GetDirectoryName(path))) + outPath;

                    if (File.Exists(outPath))
                    {
                        File.Delete(outPath);
                    }
                }
            }
        }