Esempio n. 1
0
 private SftpClientWrapper initSFTP(FTPK_FTPs site)
 {
     if (site.AuthTypeCode == VMAuthType.SSH_KEY)
     {
         string sshFilePath = Server.MapPath(site.SSHKeyPath);
         return(new SftpClientWrapper(site.Host, site.UserName, site.Password, site.Port, sshFilePath, site.SSHKeyPassword));
     }
     else
     {
         return(new SftpClientWrapper(site.Host, site.UserName, site.Password, site.Port));
     }
 }
Esempio n. 2
0
        public ActionResult Upload(int?id, string path = "")
        {
            if (!id.HasValue)
            {
                return(RedirectToAction("Index"));
            }
            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.canUpload(site.ID))
            {
                return(new HttpStatusCodeResult(HttpStatusCode.Unauthorized));
            }

            VMUpload model = new VMUpload(id.Value, path, site.DisplayName);

            return(View(model));
        }
Esempio n. 3
0
        public ActionResult Browse(int?id, string path = "")
        {
            if (!id.HasValue)
            {
                return(RedirectToAction("Index"));
            }

            FTPK_FTPs site = dbContext.FTPK_FTPs.Where(x => x.ID == id).FirstOrDefault();

            if (site == null)
            {
                return(HttpNotFound());
            }

            if (!SecurityManager.canRead(site.ID))
            {
                return(new HttpStatusCodeResult(HttpStatusCode.Unauthorized));
            }

            SftpClientWrapper sftp = initSFTP(site);

            AppResponse <List <VMDirectoryItem> > resp = sftp.getDirectoryContents(path);
            VMBrowse model = new VMBrowse();

            if (resp.success)
            {
                List <VMDirectoryItem> items       = (List <VMDirectoryItem>)resp.getData();
                VMSFTPPermission       permissions = SecurityManager.getPermissions(site.ID);
                model          = new VMBrowse(items.OrderBy(x => x.typeSequence).ThenBy(x => x.name).ToList(), id.Value, path, getPreviousPath(path), site.DisplayName, permissions);
                model.navLinks = getNavigationTreeLinks(path);
            }
            else
            {
                model.errorMessage = "Something when wrong. " + resp.message;
            }
            return(View(model));
        }
Esempio n. 4
0
        public JsonResult FetchFile(int id, string remoteDir, string fileName)
        {
            AppResponse <String> resp = new AppResponse <String>();
            FTPK_FTPs            site = dbContext.FTPK_FTPs.Where(x => x.ID == id).FirstOrDefault();

            if (site == null)
            {
                resp.SetFailure("Invalid FTP Site");
                return(Json(resp));
            }

            if (!SecurityManager.canRead(site.ID))
            {
                resp.SetFailure("Unauthorized");
                return(Json(resp));
            }


            string downloadPath = Server.MapPath(localFileDir);

            if (!Directory.Exists(downloadPath))
            {
                Directory.CreateDirectory(downloadPath);
            }
            DirectoryInfo downloadDir   = new DirectoryInfo(downloadPath);
            string        localFilePath = Path.Combine(downloadPath, fileName);

            try
            {
                bool useCacheFile           = false;
                SftpClientWrapper sftp      = initSFTP(site);
                FileInfo          localFile = downloadDir.GetFiles().Where(x => x.Name == fileName).FirstOrDefault();
                if (localFile != null)
                {
                    AppResponse <SftpFile> fileResp = sftp.getRemoteFileInfo(remoteDir, fileName);
                    if (fileResp.success)
                    {
                        SftpFile remoteFile = fileResp.getData();
                        if (remoteFile.Length == localFile.Length && remoteFile.LastWriteTime <= localFile.LastWriteTime)
                        {
                            useCacheFile = true;
                            resp.SetSuccess();
                        }
                        else
                        {
                            localFile.Delete();
                        }
                    }
                    else
                    {
                        localFile.Delete();
                    }
                }

                if (!useCacheFile)
                {
                    bool res = sftp.downloadFile(remoteDir, fileName, downloadDir);
                    if (res)
                    {
                        resp.SetSuccess();
                    }
                    else
                    {
                        resp.SetFailure("Failed to download file. Please try again later.");
                    }
                }
            }
            catch (Exception ex)
            {
                resp.SetFailure(ex.Message);
            }
            finally
            {
                //kick off cache cleanup
                Task.Factory.StartNew(this.CleanUpCache);
            }
            return(Json(resp));
        }
Esempio n. 5
0
        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));
        }
Esempio n. 6
0
        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));
        }