示例#1
0
        public EstructuraFicheros ParseJson(JObject json)
        {
            try
            {
                _estructura = SimpleInyectorEngine.Get <EstructuraFicheros>();
                Parsea(json, n =>
                {
                    if (n.First != null && n.First.Type == JTokenType.Property && ((JProperty)n.First).Name.Equals("md5"))
                    {
                        _path_actual = Regex.Replace(n.Path, @" ?\[.*?\]", string.Empty).Replace(".", @"\");

                        if (!_path_actual.Equals(_path) && !string.IsNullOrEmpty(_path))
                        {
                            _estructura.AddFicheros(_path, _ficheros);
                            _ficheros = new List <IDictionary <string, string> >();
                        }
                        _path        = _path_actual;
                        _propiedades = new Dictionary <string, string>();
                        foreach (JProperty prop in n.Children())
                        {
                            _propiedades.Add(prop.Name, (string)prop.Value);
                        }
                        _ficheros.Add(_propiedades);
                    }
                    else if (n.First.Last.Type == JTokenType.Array && !n.First.Last.HasValues)
                    {
                        string path_vacio = Regex.Replace(n.First.Last.Path, @" ?\[.*?\]", string.Empty).Replace(".", @"\");
                        _estructura.AddFicheros(path_vacio, null);
                    }
                });
                if (!_estructura.GetEstructura().ContainsKey(_path))
                {
                    _estructura.AddFicheros(_path, _ficheros);
                }
            }
            catch (Exception ex)
            {
                _estructura = null;
                _logger.Error($"Error al parsear el json\n{json.ToString()}");
                throw ex;
            }

            return(_estructura);
        }
示例#2
0
        public bool CrearEstructuraDirectorios(EstructuraFicheros estructura)
        {
            bool   ret  = true;
            string ruta = string.Empty;

            try
            {
                ruta      = estructura.Root;
                _dir_root = new DirectoryInfo(ruta);
                if (!_dir_root.Exists)
                {
                    _dir_root.Create();
                }

                foreach (KeyValuePair <string, IList <IDictionary <string, string> > > dir in estructura.GetEstructura())
                {
                    ruta = _dir_root.FullName + @"\" + dir.Key;
                    if (!Directory.Exists(ruta))
                    {
                        _dir_root.CreateSubdirectory(dir.Key);
                    }
                }
            }

            catch (Exception ex) when(ex is IOException)
            {
                throw new DirectorioToDisKException($"no se ha podido crear la ruta {ruta}", ex);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return(ret);
        }
示例#3
0
        private IList <string> GetFilesToDownload(EstructuraFicheros estructura)
        {
            IList <string> files_to_download;

            Uri    aux_uri = new Uri(estructura.UrlJson, UriKind.Absolute);
            string url     = aux_uri.AbsoluteUri.Replace(aux_uri.AbsolutePath, "") + "/" + "getfile?file=";

            files_to_download = new List <string>();

            foreach (KeyValuePair <string, IList <IDictionary <string, string> > > ruta in estructura.GetEstructura())
            {
                string path_estructura = ruta.Key;
                IList <IDictionary <string, string> > ficheros_estructura = ruta.Value;

                if (ficheros_estructura != null)
                {
                    foreach (IDictionary <string, string> propiedades_fichero in ficheros_estructura)
                    {
                        if (propiedades_fichero["required"].ToLower().Equals("true"))
                        {
                            string file_to_download = estructura.Root + path_estructura + @"\" + propiedades_fichero["name"];

                            if (!File.Exists(file_to_download))
                            {
                                files_to_download.Add(path_estructura + @"\" + propiedades_fichero["name"]);
                            }
                            else
                            {
                                using (var md5 = MD5.Create())
                                {
                                    using (var stream = File.OpenRead(file_to_download))
                                    {
                                        string hash = BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", String.Empty);

                                        if (!hash.ToLower().SequenceEqual(propiedades_fichero["md5"].ToLower()))
                                        {
                                            files_to_download.Add(path_estructura + @"\" + propiedades_fichero["name"]);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return(files_to_download);
        }