コード例 #1
0
        public override IList <IElement> End(IWorkerContext ctx, Tag tag, IList <IElement> currentContent)
        {
            IDictionary <string, string> attributes = tag.Attributes;
            string src;

            if (!attributes.TryGetValue(itsXmlHtml.HTML.Attribute.SRC, out src))
            {
                return(new List <IElement>(1));
            }

            if (string.IsNullOrEmpty(src))
            {
                return(new List <IElement>(1));
            }

            //convert url to base64 image
            if (!src.StartsWith("data:image/", StringComparison.InvariantCultureIgnoreCase))
            {
                int index = src.ToLower().IndexOf("fileid=");

                if (index >= 0)
                {
                    string newSrc = src.Substring(index + "fileid=".Length);
                    if (newSrc.IndexOf("&") > 0)
                    {
                        newSrc = newSrc.Substring(0, newSrc.IndexOf("&"));
                    }

                    Guid?       fileId = PublicMethods.parse_guid(newSrc);
                    DocFileInfo fi     = DocumentsController.get_file(ApplicationID, fileId.Value);
                    if (fi != null)
                    {
                        fi.refresh_folder_name();
                    }

                    if (!fi.exists(ApplicationID))
                    {
                        try
                        {
                            System.Drawing.Image img = null;

                            using (MemoryStream stream = new MemoryStream(fi.toByteArray(ApplicationID)))
                                img = System.Drawing.Bitmap.FromStream(stream);

                            string strWidth = tag == null || tag.CSS == null || !tag.CSS.ContainsKey("width") ?
                                              string.Empty : tag.CSS["width"];
                            string strHeight = tag == null || tag.CSS == null || !tag.CSS.ContainsKey("height") ?
                                               string.Empty : tag.CSS["height"];

                            if (!string.IsNullOrEmpty(strWidth))
                            {
                                strWidth = strWidth.ToLower().Replace("px", "");
                            }
                            if (!string.IsNullOrEmpty(strHeight))
                            {
                                strHeight = strHeight.ToLower().Replace("px", "");
                            }

                            int width = 0, height = 0, maxWidth = 650, maxHeight = 900;

                            if (string.IsNullOrEmpty(strWidth) ||
                                !int.TryParse(strWidth, out width) || width < 0)
                            {
                                width = img.Width;
                            }
                            if (string.IsNullOrEmpty(strHeight) ||
                                !int.TryParse(strHeight, out height) || height < 0)
                            {
                                height = img.Height;
                            }

                            double coeff = Math.Min(width <= maxWidth ? 1 : (double)maxWidth / (double)width,
                                                    height <= maxHeight ? 1 : (double)maxHeight / (double)height);

                            width  = (int)Math.Floor(coeff * (double)width);
                            height = (int)Math.Floor(coeff * (double)height);

                            if (width != img.Width || height != img.Height)
                            {
                                string msg = string.Empty;
                                if (RVGraphics.make_thumbnail(img, width, height, 0, 0, ref img, ref msg))
                                {
                                    tag.CSS["width"]  = (width = img.Width).ToString() + "px";
                                    tag.CSS["height"] = (height = img.Height).ToString() + "px";
                                }
                            }

                            newSrc = PublicMethods.image_to_base64(img, System.Drawing.Imaging.ImageFormat.Png);

                            if (!string.IsNullOrEmpty(newSrc))
                            {
                                src = "data:image/png;base64," + newSrc;
                            }
                        }
                        catch { }
                    }
                }
            }
            //end of convert url to base64 image

            if (src.StartsWith("data:image/", StringComparison.InvariantCultureIgnoreCase))
            {
                // data:[<MIME-type>][;charset=<encoding>][;base64],<data>
                var base64Data = src.Substring(src.IndexOf(",") + 1);
                var imagedata  = Convert.FromBase64String(base64Data);
                var image      = iTextSharp.text.Image.GetInstance(imagedata);

                image.ScaleToFitLineWhenOverflow = true;
                image.ScaleToFitHeight           = false;
                var      list = new List <IElement>();
                var      htmlPipelineContext = GetHtmlPipelineContext(ctx);
                IElement imgElement          = GetCssAppliers().Apply(new Chunk((iTextSharp.text.Image)GetCssAppliers()
                                                                                .Apply(image, tag, htmlPipelineContext), 0, 0, true), tag, htmlPipelineContext);

                list.Add(imgElement);
                return(list);
            }
            else
            {
                return(base.End(ctx, tag, currentContent));
            }
        }
コード例 #2
0
        public void ProcessRequest(HttpContext context)
        {
            paramsContainer = new ParamsContainer(context, nullTenantResponse: false);

            if (!paramsContainer.CurrentUserID.HasValue)
            {
                paramsContainer.return_response(PublicConsts.BadRequestResponse);
                return;
            }

            if (ProcessTenantIndependentRequest(context))
            {
                return;
            }

            if (!paramsContainer.ApplicationID.HasValue)
            {
                paramsContainer.return_response(PublicConsts.NullTenantResponse);
                return;
            }

            string responseText = string.Empty;

            string command = !string.IsNullOrEmpty(context.Request.Params["cmd"]) ? context.Request.Params["cmd"].ToLower() :
                             (string.IsNullOrEmpty(context.Request.Params["Command"]) ? "uploadfile" : context.Request.Params["Command"].ToLower());

            Guid userId = string.IsNullOrEmpty(context.Request.Params["UserID"]) ? PublicMethods.get_current_user_id() :
                          Guid.Parse(context.Request.Params["UserID"]);

            Guid ownerId = string.IsNullOrEmpty(context.Request.Params["OwnerID"]) ? Guid.Empty :
                           Guid.Parse(context.Request.Params["OwnerID"]);
            Guid fileId = string.IsNullOrEmpty(context.Request.Params["FileID"]) ? Guid.Empty :
                          Guid.Parse(context.Request.Params["FileID"]);
            string fileName = string.IsNullOrEmpty(context.Request.Params["FileName"]) ? string.Empty :
                              context.Request.Params["FileName"];

            FileOwnerTypes ownerType = FileOwnerTypes.None;

            if (!Enum.TryParse <FileOwnerTypes>(context.Request.Params["OwnerType"], out ownerType))
            {
                ownerType = FileOwnerTypes.None;
            }

            switch (command)
            {
            case "uploadfile":
            case "upload_file":
            {
                DocFileInfo file = new DocFileInfo()
                {
                    FileID    = fileId != Guid.Empty ? fileId : Guid.NewGuid(),
                    OwnerID   = ownerId,
                    OwnerType = ownerType
                };

                byte[] fileContent = new byte[0];

                _attach_file_command(paramsContainer.ApplicationID, file, ref responseText, ref fileContent);
                _return_response(ref responseText);
                return;
            }

            case "deletefile":
                responseText = remove_file(fileId, ref responseText) ? "yes" : "no";
                _return_response(ref responseText);
                return;

            case "removefile":
                remove_file(fileId, ref responseText);
                _return_response(ref responseText);
                return;

            case "uploadpicture":
            {
                int         maxWidth = 100, maxHeight = 100;
                FolderNames imageFolder = FolderNames.Pictures;

                Guid?pictureId = PublicMethods.parse_guid(context.Request.Params["PictureID"]);
                bool hasId     = pictureId.HasValue;
                if (!pictureId.HasValue)
                {
                    pictureId = Guid.NewGuid();
                }

                string imageType = string.IsNullOrEmpty(context.Request.Params["Type"]) ? "" :
                                   context.Request.Params["Type"].ToLower();

                string errorMessage = string.Empty;

                switch (imageType)
                {
                case "post":
                    maxWidth    = maxHeight = 640;
                    imageFolder = FolderNames.Pictures;
                    break;

                default:
                    _return_response(ref responseText);
                    return;
                }

                DocFileInfo uploaded = new DocFileInfo()
                {
                    FileID     = Guid.NewGuid(),
                    OwnerType  = ownerType,
                    FolderName = FolderNames.TemporaryFiles
                };

                if (ownerId != Guid.Empty)
                {
                    uploaded.OwnerID = ownerId;
                }

                byte[] fileContent = new byte[0];

                bool succeed = _attach_file_command(paramsContainer.ApplicationID,
                                                    uploaded, ref responseText, ref fileContent, dontStore: true);

                if (succeed)
                {
                    DocFileInfo destFile = new DocFileInfo()
                    {
                        FileID     = hasId ? pictureId : uploaded.FileID,
                        OwnerType  = ownerType,
                        Extension  = "jpg",
                        FolderName = hasId ? imageFolder : FolderNames.TemporaryFiles
                    };

                    if (ownerId != Guid.Empty)
                    {
                        destFile.OwnerID = ownerId;
                    }

                    byte[] destContent = new byte[0];

                    RVGraphics.make_thumbnail(paramsContainer.Tenant.Id, fileContent, destFile, ref destContent,
                                              maxWidth, maxHeight, 0, 0, ref errorMessage, "jpg");

                    if (string.IsNullOrEmpty(errorMessage))
                    {
                        responseText = responseText.Replace(uploaded.FileID.ToString(), destFile.FileID.ToString());
                    }
                }

                responseText = responseText.Replace("\"~[[MSG]]\"",
                                                    string.IsNullOrEmpty(errorMessage) ? "\"\"" : errorMessage);

                _return_response(ref responseText);
                return;
            }
            }

            paramsContainer.return_response(PublicConsts.BadRequestResponse);
        }