public AppResponse <SftpFile> getRemoteFileInfo(string remoteDir, string remoteFileName) { AppResponse <SftpFile> resp = new AppResponse <SftpFile>(); try { sftp.ConnectionInfo.Timeout = TimeSpan.FromMinutes(30); sftp.Connect(); SftpFile remoteFile = sftp.ListDirectory(remoteDir).Where(x => x.Name == remoteFileName).FirstOrDefault(); if (remoteFile != null) { resp.SetSuccess(); resp.setData(remoteFile); } else { resp.SetFailure("File not found"); resp.responseCode = AppResponse <SftpFile> .NOT_FOUND; } } catch (Exception ex) { resp.SetFailure(ex.Message); } finally { sftp.Disconnect(); } return(resp); }
private AppResponse <List <string> > uploadFiles(HttpPostedFileBase[] files) { AppResponse <List <string> > resp = new AppResponse <List <string> >(); //Uploading files if (files != null) { try { List <string> filesToUpload = new List <string>(); foreach (var f in files) { if (f != null) { Regex rgx = new Regex("[^a-zA-Z0-9 -_.]"); var uploadDir = localFileDir + "\\"; string fullPath = Server.MapPath(localFileDir); if (!Directory.Exists(fullPath)) { Directory.CreateDirectory(fullPath); } var filePathTemp = Path.Combine(fullPath, f.FileName); var fileNameNoEx = rgx.Replace(Path.GetFileNameWithoutExtension(filePathTemp), ""); var fileEx = Path.GetExtension(filePathTemp); string fileNameInc = ""; int inc = 0; var filePath = Path.Combine(fullPath, fileNameNoEx + fileNameInc + fileEx); string fName = fileNameNoEx + fileNameInc + fileEx; while (System.IO.File.Exists(filePath)) { inc++; fileNameInc = inc.ToString(); filePath = Path.Combine(fullPath, fileNameNoEx + fileNameInc + fileEx); fName = fileNameNoEx + fileNameInc + fileEx; } f.SaveAs(filePath); filesToUpload.Add(fName); } } resp.SetSuccess(); resp.setData(filesToUpload); } catch (Exception ex) { resp.SetFailure(ex.Message); } } return(resp); }
public AppResponse <List <VMDirectoryItem> > getDirectoryContents(string remoteDirectory) { AppResponse <List <VMDirectoryItem> > resp = new AppResponse <List <VMDirectoryItem> >(); try { sftp.ConnectionInfo.Timeout = TimeSpan.FromMinutes(30); sftp.Connect(); sftp.ChangeDirectory("/"); List <VMDirectoryItem> items = new List <VMDirectoryItem>(); var dirObjects = sftp.ListDirectory(remoteDirectory); foreach (var dir in dirObjects) { string d = dir.FullName; if (!d.EndsWith("/.") && !d.Contains("..")) { string typeCode = VMDirectoryItemType.FILE; int typeSequence = 1; if (dir.IsDirectory) { typeCode = VMDirectoryItemType.DIRECTORY; typeSequence = 0; } string[] parts = d.Split('/'); string val = parts[parts.Length - 1]; items.Add(new VMDirectoryItem(dir, remoteDirectory, typeCode, typeSequence)); } } resp.SetSuccess(); resp.setData(items); } catch (Exception ex) { resp.SetFailure(ex.Message); } return(resp); }