private async Task <Resultado> Descarga(EstructuraFicheros estructura) { Resultado ret = SimpleInyectorEngine.Get <Resultado>(); IList <string> filesToDownload = GetFilesToDownload(estructura); if (filesToDownload != null) { _controlesProgeso.pbarProgeso.Minimum = 0; _controlesProgeso.pbarProgeso.Maximum = filesToDownload.Count(); _controlesProgeso.pbarProgeso.BeginAnimation(RangeBase.ValueProperty, null); _saveFiles.FicheroProcesadoEvent += saveFiles_FicheroProcesadoEvent; if (estructura != null) { if (_directorio.CrearEstructuraDirectorios(estructura)) { ret = await _saveFiles.Save(filesToDownload, estructura, _conErrores); if (!ret.ProcesoCorrecto) { _serializacion.Serializa(estructura); } } } _saveFiles.FicheroProcesadoEvent -= saveFiles_FicheroProcesadoEvent; } return(ret); }
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); }
public async Task <Resultado> RecuperaErrores(ControlesProgeso controlesProgeso, bool conErrores) { _controlesProgeso = controlesProgeso; _conErrores = conErrores; EstructuraFicheros estructura = _serializacion.Deserializa <EstructuraFicheros>(); if (!estructura.Root.EndsWith(@"\")) { estructura.Root += @"\"; } return(await Descarga(estructura)); }
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); }
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); }
public async Task <Resultado> Save(IList <string> filesToDownload, EstructuraFicheros estructura, bool conErrores) { _resultado = SimpleInyectorEngine.Get <Resultado>(); Uri aux_uri = new Uri(estructura.UrlJson, UriKind.Absolute); string url = aux_uri.AbsoluteUri.Replace(aux_uri.AbsolutePath, "") + "/" + "getfile?file="; //pbar.Maximum = filesToDownload.Count(); //pbar.Minimum = 0; //pbar.BeginAnimation(RangeBase.ValueProperty, null); IList <string> ficheros_ok = await GetFicherosFromApi(estructura.Root, url, filesToDownload, conErrores); estructura.LimpiaEstructura(ficheros_ok); _resultado.Procesados = filesToDownload.Count(); _resultado.Correctos = _resultado.Procesados - _resultado.Fallidos; _resultado.ProcesoCorrecto = _resultado.Correctos == _resultado.Procesados; return(_resultado); }
public async Task <Resultado> DownloadManagerStart(string urlJson, string rutaDestino, ControlesProgeso controlesProgeso, bool conErrores) { _controlesProgeso = controlesProgeso; _conErrores = conErrores; if (!rutaDestino.EndsWith(@"\")) { rutaDestino += @"\"; } JObject json = _jsonManager.GetJson(urlJson); if (json != null) { EstructuraFicheros estructura = _jsonManager.ParseJson(json); estructura.UrlJson = urlJson; estructura.Root = rutaDestino; return(await Descarga(estructura)); } else { return(SimpleInyectorEngine.Get <Resultado>()); } }