Пример #1
0
        private static byte[] GetBinaryDataFromUrl(KeyAndId ki)
        {
            byte[] data = null;

            try
            {
                using (Devmasters.Net.Web.URLContent net = new Devmasters.Net.Web.URLContent(ki.ValueForData))
                {
                    net.Timeout          = 7000;
                    net.IgnoreHttpErrors = true;
                    data = net.GetBinary().Binary;
                }
            }
            catch (Exception e)
            {
                HlidacStatu.Util.Consts.Logger.Error("Manager Save", e);
            }
            if (data == null || data.Length == 0)
            {
                return(System.IO.File.ReadAllBytes(HlidacStatu.Lib.Init.WebAppRoot + @"content\icons\largetile.png"));
            }
            else
            {
                return(data);
            }
        }
Пример #2
0
        private static string DownloadStatement(string statementUrl, string root)
        {
            var pdfTmpFile = Path.Combine(root, "statement.pdf");

            using (var net = new Devmasters.Net.Web.URLContent(statementUrl))
            {
                File.WriteAllBytes(pdfTmpFile, net.GetBinary().Binary);
            }
            return(pdfTmpFile);
        }
                                                   TimeSpan.FromDays(365 * 10)); //10 years

            private static byte[] getRawStemsFromServer(KeyAndId smlouvaKeyId)
            {
                Smlouva s = Smlouva.Load(smlouvaKeyId.ValueForData);

                if (s == null)
                {
                    return(null);
                }

                var settings = new JsonSerializerSettings();

                settings.ContractResolver = new HlidacStatu.Util.FirstCaseLowercaseContractResolver();


                using (Devmasters.Net.Web.URLContent stem = new Devmasters.Net.Web.URLContent(classificationBaseUrl() + "/stemmer"))
                {
                    stem.Method = Devmasters.Net.Web.MethodEnum.POST;
                    stem.Tries  = 3;
                    stem.TimeInMsBetweenTries = 5000;
                    stem.Timeout     = 1000 * 60 * 30; //30min
                    stem.ContentType = "application/json; charset=utf-8";
                    stem.RequestParams.RawContent = Newtonsoft.Json.JsonConvert.SerializeObject(s, settings);
                    Devmasters.Net.Web.BinaryContentResult stems = null;
                    try
                    {
                        Util.Consts.Logger.Debug("Getting stems from " + stem.Url);

                        stems = stem.GetBinary();
                        return(stems.Binary);
                    }
                    catch (Exception e)
                    {
                        Util.Consts.Logger.Error("Classification Stemmer API error " + stem.Url, e);
                        throw;
                    }
                }
            }
Пример #4
0
        public void SaveAttachmentsToDisk(bool rewriteExisting = false)
        {
            //string root = AppDomain.CurrentDomain.BaseDirectory + "\\Prilohy\\";
            //if (!System.IO.Directory.Exists(root))
            //    System.IO.Directory.CreateDirectory(root);


            //string dir = root + item.Id;
            //if (!System.IO.Directory.Exists(dir))
            //{
            //    System.IO.Directory.CreateDirectory(dir);
            //}
            var io = Lib.Init.PrilohaLocalCopy;

            int    count   = 0;
            string listing = "";

            if (this.Prilohy != null)
            {
                if (!System.IO.Directory.Exists(io.GetFullDir(this)))
                {
                    System.IO.Directory.CreateDirectory(io.GetFullDir(this));
                }

                foreach (var p in this.Prilohy)
                {
                    string attUrl = p.odkaz;
                    if (string.IsNullOrEmpty(attUrl))
                    {
                        continue;
                    }
                    count++;
                    string fullPath = io.GetFullPath(this, p);
                    listing = listing + string.Format("{0} : {1} \n", count, System.Net.WebUtility.UrlDecode(attUrl));
                    if (!System.IO.File.Exists(fullPath) || rewriteExisting)
                    {
                        try
                        {
                            using (Devmasters.Net.Web.URLContent url = new Devmasters.Net.Web.URLContent(attUrl))
                            {
                                url.Timeout = url.Timeout * 10;
                                byte[] data = url.GetBinary().Binary;
                                System.IO.File.WriteAllBytes(fullPath, data);
                                //p.LocalCopy = System.Text.UTF8Encoding.UTF8.GetBytes(io.GetRelativePath(item, p));
                            }
                        }
                        catch (Exception e)
                        {
                            HlidacStatu.Util.Consts.Logger.Error(attUrl, e);
                        }
                    }
                    if (p.hash == null)
                    {
                        using (FileStream filestream = new FileStream(fullPath, FileMode.Open))
                        {
                            using (SHA256 mySHA256 = SHA256Managed.Create())
                            {
                                filestream.Position = 0;
                                byte[] hashValue = mySHA256.ComputeHash(filestream);
                                p.hash = new Lib.XSD.tHash()
                                {
                                    algoritmus = "sha256",
                                    Value      = BitConverter.ToString(hashValue).Replace("-", String.Empty)
                                };
                            }
                        }
                    }

                    //System.IO.File.WriteAllText(dir + "\\" + "content.nfo", listing);
                }
            }
        }
Пример #5
0
            public static string GetFileFromPrilohaRepository(HlidacStatu.Lib.Data.Smlouva.Priloha att,
                                                              Lib.Data.Smlouva smlouva)
            {
                var ext = ".pdf";

                try
                {
                    ext = new System.IO.FileInfo(att.nazevSouboru).Extension;
                }
                catch (Exception e)
                {
                    HlidacStatu.Util.Consts.Logger.Warning("invalid file name " + (att?.nazevSouboru ?? "(null)"));
                }


                string localFile = Lib.Init.PrilohaLocalCopy.GetFullPath(smlouva, att);
                var    tmpPath   = System.IO.Path.GetTempPath();

                HlidacStatu.Util.IOTools.DeleteFile(tmpPath);
                if (!System.IO.Directory.Exists(tmpPath))
                {
                    try
                    {
                        System.IO.Directory.CreateDirectory(tmpPath);
                    }
                    catch
                    {
                    }
                }
                string tmpFnSystem = System.IO.Path.GetTempFileName();
                string tmpFn       = tmpFnSystem + HlidacStatu.Lib.OCR.DocTools.PrepareFilenameForOCR(att.nazevSouboru);

                try
                {
                    //System.IO.File.Delete(fn);
                    if (System.IO.File.Exists(localFile))
                    {
                        //do local copy
                        Consts.Logger.Debug($"Copying priloha {att.nazevSouboru} for smlouva {smlouva.Id} from local disk {localFile}");
                        System.IO.File.Copy(localFile, tmpFn, true);
                    }
                    else
                    {
                        try
                        {
                            Consts.Logger.Debug($"Downloading priloha {att.nazevSouboru} for smlouva {smlouva.Id} from URL {att.odkaz}");
                            byte[] data = null;
                            using (Devmasters.Net.Web.URLContent web = new Devmasters.Net.Web.URLContent(att.odkaz))
                            {
                                web.Timeout = web.Timeout * 10;
                                data        = web.GetBinary().Binary;
                                System.IO.File.WriteAllBytes(tmpFn, data);
                            }
                            Consts.Logger.Debug($"Downloaded priloha {att.nazevSouboru} for smlouva {smlouva.Id} from URL {att.odkaz}");
                        }
                        catch (Exception)
                        {
                            try
                            {
                                byte[] data = null;
                                Consts.Logger.Debug($"Second try: Downloading priloha {att.nazevSouboru} for smlouva {smlouva.Id} from URL {att.odkaz}");
                                using (Devmasters.Net.Web.URLContent web = new Devmasters.Net.Web.URLContent(att.odkaz))
                                {
                                    web.Tries                = 5;
                                    web.IgnoreHttpErrors     = true;
                                    web.TimeInMsBetweenTries = 1000;
                                    web.Timeout              = web.Timeout * 20;
                                    data = web.GetBinary().Binary;
                                    System.IO.File.WriteAllBytes(tmpFn, data);
                                }
                                Consts.Logger.Debug($"Second try: Downloaded priloha {att.nazevSouboru} for smlouva {smlouva.Id} from URL {att.odkaz}");
                                return(tmpFn);
                            }
                            catch (Exception e)
                            {
                                HlidacStatu.Util.Consts.Logger.Error(att.odkaz, e);
                                return(null);
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    HlidacStatu.Util.Consts.Logger.Error(att.odkaz, e);
                    throw;
                }
                finally
                {
                    HlidacStatu.Util.IOTools.DeleteFile(tmpFnSystem);
                }
                return(tmpFn);
            }