public ActionResult DownloadFile(DownloadMetaData info) { DownloadResult result = null; if (ModelState.IsValid) { try { info.FileName = GetUniqueName(info.downloadUrl); string destinationPath = pathProvider.MapPath(info.FileName); info.FileType = GetFileTye(info.downloadUrl); string requestType = GetDownloadType(info.downloadUrl); requestType = requestType.Contains("https") == true ? "http" : requestType; switch (requestType) { case "http": _protocolStrategy = new HttpProtocolDownloader(); _downloader = new Downloader(_protocolStrategy); result = _downloader.Download(info.downloadUrl, info.ParallelDownloads, destinationPath); break; case "ftp": _protocolStrategy = new FtpProtocolDownloader(iConfig.GetSection("MyFtpSetting").GetSection("NetworkHostName").Value, iConfig.GetSection("MyFtpSetting").GetSection("Password").Value); _downloader = new Downloader(_protocolStrategy); result = _downloader.Download(info.downloadUrl, info.ParallelDownloads, destinationPath); break; default: _protocolStrategy = null; break; } if (_protocolStrategy == null) { ModelState.AddModelError("ProtocolError", "This protocol is currently not supported"); return(View()); } else { if (result == null) { ModelState.AddModelError("DownloadError", "Something went wrong, Please try again"); return(View()); } info.FilePath = destinationPath.ToString(); info.Size = result.Size; info.TimeTaken = result.TimeTaken; info.Status = "Processing"; info.ContentType = result.ContentType; downloadRepo.SaveDownloadDetails(info); var details = downloadRepo.GetAllDownloads(); return(View(details)); } } catch (Exception ex) { ModelState.AddModelError("Error", "Please try again check your source url"); return(View()); } } else { return(RedirectToAction(nameof(Index))); } }
public Downloader(IProtocolStrategy protocolStrategyl) { this._protocolStrategy = protocolStrategyl; }