示例#1
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));
        }
示例#2
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));
        }
示例#3
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));
        }