public ActionResult Upload(VMUpload model) { if (model == null) { return(HttpNotFound()); } if (model.path == null) { model.path = ""; } AppResponse <List <string> > resp = uploadFiles(model.files); if (!resp.success) { ModelState.AddModelError("files", resp.message); return(View(model)); } List <string> filesToUpload = resp.getData(); FTPK_FTPs site = dbContext.FTPK_FTPs.Where(x => x.ID == model.id).FirstOrDefault(); if (site == null) { return(HttpNotFound()); } if (!SecurityManager.canUpload(site.ID)) { return(new HttpStatusCodeResult(HttpStatusCode.Unauthorized)); } SftpClientWrapper sftp = initSFTP(site); string fullPath = Server.MapPath(localFileDir); DirectoryInfo localDir = new DirectoryInfo(fullPath); int failedCnt = 0; foreach (string fName in filesToUpload) { try { FTPK_Logs log = new FTPK_Logs(); log.SiteID = model.id; log.Action = ACTION_UPLOAD; log.FileName = fName; log.Path = model.path; log.UserID = SecurityManager.getUserID(); log.LogDate = DateTime.Now; dbContext.FTPK_Logs.Add(log); dbContext.SaveChanges(); resp.SetSuccess(); } catch (Exception ex) { resp.SetFailure(ex.Message); } bool success = sftp.uploadFile(model.path, fName, localDir); if (!success) { ModelState.AddModelError("files", fName + " failed to upload. Please retry this file."); failedCnt++; } } AppResponse <Object> mResp = new AppResponse <object>(); if (!ModelState.IsValid) { mResp.SetFailure(failedCnt + " of " + filesToUpload.Count() + " file(s) uploaded successfully!"); } else { mResp.SetSuccess(); } model.files = null; model.response = mResp; return(View(model)); }
public ActionResult Download(int?id, string remoteDir, string fileName) { AppResponse <Object> resp = new AppResponse <object>(); if (remoteDir == null) { remoteDir = ""; } if (!id.HasValue) { return(RedirectToAction("Index")); } if (fileName == "") { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } string siteName = ""; try { FTPK_FTPs site = dbContext.FTPK_FTPs.Where(x => x.ID == id.Value).FirstOrDefault(); if (site == null) { return(HttpNotFound("No FTP Site with ID '" + id.Value + ", found")); } if (!SecurityManager.canRead(site.ID)) { return(new HttpStatusCodeResult(HttpStatusCode.Unauthorized)); } siteName = site.DisplayName; FTPK_Logs log = new FTPK_Logs(); log.SiteID = id.Value; log.Action = ACTION_DOWNLOAD; log.FileName = fileName; log.Path = remoteDir; log.UserID = SecurityManager.getUserID(); log.LogDate = DateTime.Now; dbContext.FTPK_Logs.Add(log); dbContext.SaveChanges(); resp.SetSuccess(); } catch (DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors) { Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; } catch (Exception ex) { resp.SetFailure(ex.Message); } VMDownload model = new VMDownload(id.Value, remoteDir, fileName, siteName); model.response = resp; return(View(model)); }