Exemplo n.º 1
0
        public dynamic ConvertSave(string FilePath, int Id, Stream stream, string Suffix = null)
        {
            dynamic result = "failed";

            using (MemoryStream memoryStream = new MemoryStream())
            {
                if (stream != null)
                {
                    List <Setting> settings = Managers.SettingManager.GetSettings(PortalSettings.PortalId, 0, "security_settings");
                    string         Picture_DefaultFolder = "-1";
                    if (settings != null && settings.Count > 0 && settings.Where(s => s.Name == "Picture_DefaultFolder").FirstOrDefault() != null)
                    {
                        Picture_DefaultFolder = settings.Where(s => s.Name == "Picture_DefaultFolder").FirstOrDefault().Value;
                    }

                    IFolderInfo folderInfo = FolderManager.Instance.GetFolder(Picture_DefaultFolder == "-1" ? BrowseUploadFactory.GetRootFolder(PortalSettings.PortalId).FolderID : int.Parse(Picture_DefaultFolder));
                    if (folderInfo != null && BrowseUploadFactory.HasPermission(folderInfo, "WRITE"))
                    {
                        string FileName = Security.ReplaceIllegalCharacters(Path.GetFileName(FilePath));
                        if (Id > -1)
                        {
                            FileName = Id + FileName.Substring(FileName.LastIndexOf('.'));
                        }

                        string FileExtension = FileName.Substring(FileName.LastIndexOf('.'));

                        //Force extention to be .png to maintain transparency
                        FileName      = FileName.Remove(FileName.Length - FileExtension.Length) + ".png";
                        FileExtension = ".png";

                        string TempFileName = FileName;

                        if (Security.IsAllowedExtension(FileExtension, FileSetting.FileType.Split(',')))
                        {
                            int count = 1;

Find:
                            if (FileManager.Instance.FileExists(folderInfo, TempFileName))
                            {
                                string FName = FileName.Remove(FileName.Length - FileExtension.Length);
                                if (!string.IsNullOrEmpty(Suffix))
                                {
                                    if (FileName.Contains(Suffix))
                                    {
                                        string[] FInfo = FileName.Split(new string[] { Suffix }, StringSplitOptions.None);
                                        if (FInfo.Length > 1)
                                        {
                                            FName = FInfo[0];
                                        }
                                    }
                                }
                                TempFileName = FName + (!string.IsNullOrEmpty(Suffix) ? Suffix : string.Empty) + count + FileExtension;
                                count++;
                                goto Find;
                            }
                            else
                            {
                                byte[] photoBytes = ToByteArray(stream);

                                System.Drawing.Image SrcImage = System.Drawing.Image.FromStream(stream);
                                int Width = SrcImage.Width;
                                SrcImage.Dispose();

                                using (MemoryStream inStream = new MemoryStream(photoBytes))
                                {
                                    FileName = TempFileName;
                                    IFileInfo fileInfo;
                                    if (!string.IsNullOrEmpty(Suffix))
                                    {
                                        using (ImageFactory imageFactory = new ImageFactory(preserveExifData: false))
                                        {
                                            //Convert to png
                                            ISupportedImageFormat format = new PngFormat();

                                            System.Drawing.Size size = new System.Drawing.Size(Width, 0);

                                            // Load, resize, set the format and quality and save an image.
                                            imageFactory.Load(inStream)
                                            .Format(format)
                                            .Save(memoryStream);
                                            fileInfo = FileManager.Instance.AddFile(folderInfo, FileName, memoryStream);
                                            memoryStream.Seek(0, SeekOrigin.Begin);
                                        }
                                    }
                                    else
                                    {
                                        stream.CopyTo(memoryStream);
                                        fileInfo = FileManager.Instance.AddFile(folderInfo, FileName, memoryStream);
                                        memoryStream.Seek(0, SeekOrigin.Begin);
                                    }

                                    if (Utils.IsImageVersionable(fileInfo))
                                    {
                                        BrowseUploadFactory.CropImage(FileName, FileExtension, folderInfo, memoryStream);
                                    }

                                    if (fileInfo != null)
                                    {
                                        result = BrowseUploadFactory.GetUrl(fileInfo.FileId);
                                    }
                                }
                            }
                        }
                    }

                    else
                    {
                        throw new Exception("Error: You do not have write permission for folder " + (folderInfo != null ? folderInfo.FolderPath.TrimEnd('/') : string.Empty));
                    }
                }
            }
            return(result);
        }