protected bool __ie_response(Guid?applicationId, HttpPostedFile uploadedFile, DocFileInfo fi,
                                     ref string responseText, ref byte[] fileContent, bool dontStore = false)
        {
            string filename = HttpUtility.UrlDecode(uploadedFile.FileName);

            int indexOfLastDot = filename.LastIndexOf('.');

            if (indexOfLastDot >= 0 && indexOfLastDot < filename.Length - 1)
            {
                fi.Extension = filename.Substring(indexOfLastDot + 1).ToLower();
            }
            fi.FileName = string.IsNullOrEmpty(fi.Extension) ? filename : filename.Substring(0, indexOfLastDot);

            try
            {
                fi.Size = uploadedFile.ContentLength;

                using (BinaryReader reader = new BinaryReader(uploadedFile.InputStream))
                {
                    fileContent = reader.ReadBytes(uploadedFile.ContentLength);
                    if (!dontStore)
                    {
                        fi.store(applicationId, fileContent);
                    }
                }

                responseText = "{\"success\":" + true.ToString().ToLower() +
                               ",\"AttachedFile\":" + fi.toJson(applicationId, true) +
                               ",\"name\":\"" + fi.file_name_with_extension + "\"" +
                               ",\"url\":\"" + fi.url(applicationId) + "\"" +
                               ",\"Message\":\"~[[MSG]]\"}";
            }
            catch (Exception)
            {
                fi.FileID    = null;
                responseText = "{\"success\":\"false\"}";
                return(false);
            }

            return(false);
        }
        protected bool __other_browsers_response(Guid?applicationId, Stream inputStream, DocFileInfo fi,
                                                 ref string responseText, ref byte[] fileContent, bool dontStore = false)
        {
            //This work for Firefox and Chrome.

            try
            {
                Stream st = HttpContext.Current.Request.Files.Count > 0 ?
                            HttpContext.Current.Request.Files[0].InputStream : inputStream;

                using (BinaryReader reader = new BinaryReader(st))
                {
                    fileContent = reader.ReadBytes(Convert.ToInt32(st.Length));
                    if (!dontStore && !fi.store(applicationId, fileContent))
                    {
                        responseText = "{\"success\":false}";
                        return(false);
                    }
                }

                responseText = "{\"success\":" + true.ToString().ToLower() +
                               ",\"AttachedFile\":" + fi.toJson(applicationId, true) +
                               ",\"name\":\"" + fi.file_name_with_extension + "\"" +
                               ",\"url\":\"" + fi.url(applicationId) + "\"" +
                               ",\"Message\":\"~[[MSG]]\"}";
            }
            catch (Exception)
            {
                fi.FileID    = null;
                responseText = "{\"success\":false}";
                return(false);
            }
            finally
            {
                inputStream.Close();
                inputStream.Dispose();
            }

            return(true);
        }
        private void activate_node_type_icon(Guid applicationId, string id)
        {
            Guid?templateNodeTypeId = IDs.get_id_from_template(id);

            if (!templateNodeTypeId.HasValue)
            {
                return;
            }

            DocFileInfo pic = new DocFileInfo()
            {
                FileID     = templateNodeTypeId,
                Extension  = "jpg",
                FileName   = templateNodeTypeId.ToString(),
                FolderName = FolderNames.Icons
            };

            byte[] fileContent = pic.toByteArray(RaaiVanSettings.ReferenceTenantID);

            if (fileContent.Length == 0)
            {
                return;
            }

            Guid?newFileId = IDs.new_id(id);

            DocFileInfo newPic = new DocFileInfo()
            {
                FileID     = newFileId,
                Extension  = "jpg",
                FileName   = newFileId.ToString(),
                FolderName = FolderNames.Icons
            };

            newPic.store(applicationId, fileContent);
        }
        public static void pdf2image(Guid applicationId,
                                     DocFileInfo pdf, string password, DocFileInfo destFile, ImageFormat imageFormat, bool repair)
        {
            //MagickNET.SetGhostscriptDirectory("[GhostScript DLL Dir]");
            //MagickNET.SetTempDirectory("[a temp dir]");

            if (!pdf.FileID.HasValue)
            {
                return;
            }
            else if (PDF2ImageProcessing.ContainsKey(pdf.FileID.Value) &&
                     (!repair || PDF2ImageProcessing[pdf.FileID.Value]))
            {
                return;
            }
            else
            {
                PDF2ImageProcessing[pdf.FileID.Value] = true;
            }

            if (destFile.file_exists_in_folder(applicationId) && !repair)
            {
                PDF2ImageProcessing[pdf.FileID.Value] = false;
                return;
            }

            try
            {
                string cacheDir = PublicMethods.map_path(PublicConsts.MagickCacheDirectory, localPath: true);
                if (!Directory.Exists(cacheDir))
                {
                    Directory.CreateDirectory(cacheDir);
                }
                MagickAnyCPU.CacheDirectory = cacheDir;
            }
            catch (Exception ex)
            {
                LogController.save_error_log(applicationId, null, "SetMagickCacheDirectory", ex, ModuleIdentifier.DCT);
            }

            try
            {
                string tempDir = PublicMethods.map_path(PublicConsts.TempDirectory, localPath: true);
                if (!Directory.Exists(tempDir))
                {
                    Directory.CreateDirectory(tempDir);
                }
                MagickNET.SetTempDirectory(tempDir);

                if (!string.IsNullOrEmpty(RaaiVanSettings.GhostScriptDirectory))
                {
                    MagickNET.SetGhostscriptDirectory(RaaiVanSettings.GhostScriptDirectory);
                }
            }
            catch (Exception ex)
            {
                LogController.save_error_log(applicationId, null, "SetMagickTempDirectory", ex, ModuleIdentifier.DCT);
            }

            try
            {
                using (MagickImageCollection pages = new MagickImageCollection())
                {
                    MagickReadSettings settings = new MagickReadSettings()
                    {
                        Density = new Density(100, 100)
                    };

                    bool invalidPassword = false;

                    using (PdfReader reader = PDFUtil.open_pdf_file(applicationId, pdf, password, ref invalidPassword))
                    {
                        byte[] pdfContent = PDFUtil.to_byte_array(reader);
                        pages.Read(pdfContent, settings);
                    }

                    int  pageNum    = 0;
                    bool errorLoged = false;

                    foreach (MagickImage p in pages)
                    {
                        ++pageNum;

                        destFile.FileName  = pageNum.ToString();
                        destFile.Extension = imageFormat.ToString().ToLower();

                        if (destFile.exists(applicationId))
                        {
                            continue;
                        }

                        try
                        {
                            using (MemoryStream stream = new MemoryStream())
                            {
                                p.ToBitmap(imageFormat).Save(stream, imageFormat);
                                destFile.store(applicationId, stream.ToArray());
                            }
                        }
                        catch (Exception ex)
                        {
                            if (!errorLoged)
                            {
                                errorLoged = true;
                                LogController.save_error_log(applicationId, null,
                                                             "ConvertPDFPageToImage", ex, ModuleIdentifier.DCT);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogController.save_error_log(applicationId, null, "ConvertPDFToImage", ex, ModuleIdentifier.DCT);
            }

            PDF2ImageProcessing[pdf.FileID.Value] = false;
        }