コード例 #1
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);
        }
コード例 #2
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));
            }
        }
コード例 #3
0
        public bool ProcessTenantIndependentRequest(HttpContext context)
        {
            if (!RaaiVanSettings.SAASBasedMultiTenancy && !paramsContainer.ApplicationID.HasValue)
            {
                paramsContainer.return_response(PublicConsts.NullTenantResponse);
                return(true);
            }

            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"]);

            FileOwnerTypes ownerType = FileOwnerTypes.None;

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

            Guid?applicationId = paramsContainer.ApplicationID;

            switch (command)
            {
            case "uploadicon":
            {
                Guid iconId = string.IsNullOrEmpty(context.Request.Params["IconID"]) ? Guid.Empty :
                              Guid.Parse(context.Request.Params["IconID"]);

                IconType iconType = IconType.None;
                if (!Enum.TryParse <IconType>(context.Request.Params["Type"], true, out iconType))
                {
                    iconType = IconType.None;
                }

                if (iconType == IconType.ApplicationIcon || (RaaiVanSettings.SAASBasedMultiTenancy &&
                                                             (iconType == IconType.ProfileImage || iconType == IconType.CoverPhoto)))
                {
                    applicationId = null;
                }
                else if (!applicationId.HasValue)
                {
                    responseText = PublicConsts.NullTenantResponse;
                    break;
                }

                DocFileInfo uploaded = new DocFileInfo()
                {
                    FileID     = iconId,
                    OwnerID    = ownerId,
                    OwnerType  = ownerType,
                    FolderName = FolderNames.TemporaryFiles
                };

                byte[] fileContent = new byte[0];

                bool succeed = _attach_file_command(applicationId, uploaded, ref responseText, ref fileContent, dontStore: true);

                if (!succeed || fileContent == null || fileContent.Length == 0)
                {
                    break;
                }

                if (iconType == IconType.ProfileImage && iconId == Guid.Empty)
                {
                    iconId = userId;
                }

                string errorMessage = string.Empty;

                IconMeta meta = null;

                succeed = RVGraphics.create_icon(applicationId, iconId, iconType, fileContent, ref errorMessage, ref meta);

                if (succeed && meta != null)
                {
                    responseText = responseText.Replace("\"~[[MSG]]\"", meta.toJson(applicationId));
                }
                else
                {
                    responseText = responseText.Replace("\"~[[MSG]]\"", errorMessage);
                }

                try
                {
                    string tempRes = string.Empty;
                    if (succeed)
                    {
                        remove_file(uploaded, ref tempRes);
                    }
                }
                catch { }

                break;
            }

            case "deleteicon":
            {
                Guid iconId = string.IsNullOrEmpty(context.Request.Params["IconID"]) ? Guid.Empty :
                              Guid.Parse(context.Request.Params["IconID"]);

                IconType iconType = IconType.None;
                if (!Enum.TryParse <IconType>(context.Request.Params["Type"], true, out iconType))
                {
                    iconType = IconType.None;
                }

                if (iconType == IconType.ApplicationIcon || (RaaiVanSettings.SAASBasedMultiTenancy &&
                                                             (iconType == IconType.ProfileImage || iconType == IconType.CoverPhoto)))
                {
                    applicationId = null;
                }
                else if (!applicationId.HasValue)
                {
                    responseText = PublicConsts.NullTenantResponse;
                    break;
                }

                if (iconType == IconType.ProfileImage && iconId == Guid.Empty)
                {
                    iconId = userId;
                }

                FolderNames folderName            = FolderNames.ProfileImages;
                FolderNames highQualityFolderName = FolderNames.HighQualityProfileImage;

                string defaultIconUrl = string.Empty;

                bool isValid = DocumentUtilities.get_icon_parameters(applicationId, iconType,
                                                                     ref folderName, ref highQualityFolderName, ref defaultIconUrl);

                if (!isValid)
                {
                    responseText = PublicConsts.NullTenantResponse;
                    break;
                }

                new DocFileInfo()
                {
                    FileID     = iconId,
                    Extension  = "jpg",
                    OwnerID    = ownerId,
                    OwnerType  = ownerType,
                    FolderName = folderName
                }.delete(paramsContainer.ApplicationID);

                new DocFileInfo()
                {
                    FileID     = iconId,
                    Extension  = "jpg",
                    OwnerID    = ownerId,
                    OwnerType  = ownerType,
                    FolderName = highQualityFolderName
                }.delete(paramsContainer.ApplicationID);

                responseText = "{\"Succeed\":\"" + Messages.OperationCompletedSuccessfully + "\"" +
                               ",\"DefaultIconURL\":\"" + defaultIconUrl + "\"}";

                break;
            }

            case "cropicon":
            {
                Guid iconId = string.IsNullOrEmpty(context.Request.Params["IconID"]) ? Guid.Empty :
                              Guid.Parse(context.Request.Params["IconID"]);

                IconType iconType = IconType.None;
                if (!Enum.TryParse <IconType>(context.Request.Params["Type"], true, out iconType))
                {
                    iconType = IconType.None;
                }

                if (iconType == IconType.ApplicationIcon || (RaaiVanSettings.SAASBasedMultiTenancy &&
                                                             (iconType == IconType.ProfileImage || iconType == IconType.CoverPhoto)))
                {
                    applicationId = null;
                }
                else if (!applicationId.HasValue)
                {
                    responseText = PublicConsts.NullTenantResponse;
                    break;
                }

                int         iconWidth = 100, iconHeight = 100;
                FolderNames imageFolder            = FolderNames.ProfileImages,
                            highQualityImageFolder = FolderNames.HighQualityProfileImage;

                if (iconType == IconType.ProfileImage && iconId == Guid.Empty)
                {
                    iconId = userId;
                }

                bool isValid = DocumentUtilities.get_icon_parameters(applicationId, iconType,
                                                                     ref iconWidth, ref iconHeight, ref imageFolder, ref highQualityImageFolder);

                if (!isValid)
                {
                    break;
                }

                DocFileInfo highQualityFile =
                    new DocFileInfo()
                {
                    FileID = iconId, Extension = "jpg", FolderName = highQualityImageFolder
                };

                DocFileInfo file = new DocFileInfo()
                {
                    FileID = iconId, Extension = "jpg", FolderName = imageFolder
                };

                int x     = string.IsNullOrEmpty(context.Request.Params["X"]) ? -1 : int.Parse(context.Request.Params["X"]);
                int y     = string.IsNullOrEmpty(context.Request.Params["Y"]) ? -1 : int.Parse(context.Request.Params["Y"]);
                int width = string.IsNullOrEmpty(context.Request.Params["Width"]) ? -1 :
                            (int)double.Parse(context.Request.Params["Width"]);
                int height = string.IsNullOrEmpty(context.Request.Params["Height"]) ? -1 :
                             (int)double.Parse(context.Request.Params["Height"]);

                IconMeta meta = null;

                RVGraphics.extract_thumbnail(applicationId, highQualityFile, highQualityFile.toByteArray(applicationId), file,
                                             x, y, width, height, iconWidth, iconHeight, ref responseText, ref meta);

                break;
            }
            }

            if (!string.IsNullOrEmpty(responseText))
            {
                paramsContainer.return_response(ref responseText);
            }

            return(!string.IsNullOrEmpty(responseText));
        }