Skip to content

Commit

Permalink
Implemented Linux FTP adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
mazumdes committed Jan 28, 2022
1 parent dd232b0 commit b9b2f42
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 17 deletions.
12 changes: 11 additions & 1 deletion Library.Encyclopedia.API/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ public static IHostBuilder CreateHostBuilder(string[] args) =>
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
})
.ConfigureLogging((logging)=> {
// clear default logging providers
logging.ClearProviders();

// add built-in providers manually, as needed
logging.AddConsole();
logging.AddDebug();
logging.AddEventLog();
logging.AddEventSourceLogger();
});
}
}
29 changes: 22 additions & 7 deletions Library.Encyclopedia.API/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public Startup(IConfiguration configuration)
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddLogging();
services.AddCors(options =>
{
options.AddPolicy(name: MyAllowSpecificOrigins,
Expand All @@ -39,16 +40,30 @@ public void ConfigureServices(IServiceCollection services)

services.AddControllers();

string url = Configuration.GetValue<string>("FTPSettings:filePath");
string userName = Configuration.GetValue<string>("FTPSettings:username");
string password = Configuration.GetValue<string>("FTPSettings:password");
if (Configuration.GetValue<string>("FileAdapterSettings:type").Equals("linux", System.StringComparison.OrdinalIgnoreCase))
{
string url = Configuration.GetValue<string>("FileAdapterSettings:url");
string userName = Configuration.GetValue<string>("FileAdapterSettings:username");
string password = Configuration.GetValue<string>("FileAdapterSettings:password");
string basePath = Configuration.GetValue<string>("FileAdapterSettings:basePath");

services.AddScoped<IApplicationDbContext>(s => new ApplicationDbContext(Configuration.GetConnectionString("DefaultConnection")));
services.AddScoped<IFilesAdapter>(s =>
services.AddSingleton<IFilesAdapter>(s =>
{
return new LinuxFileSaveAdapter(url, userName, password, basePath);
});
}
else if(Configuration.GetValue<string>("FileAdapterSettings:type").Equals("windows", System.StringComparison.OrdinalIgnoreCase))
{
return new WindowsFileSaveAdapter(url);
});
string filePath = Configuration.GetValue<string>("FileAdapterSettings:filePath");

services.AddSingleton<IFilesAdapter>(s =>
{
return new WindowsFileSaveAdapter(filePath);
});
}

services.AddScoped<IApplicationDbContext>(s => new ApplicationDbContext(Configuration.GetConnectionString("DefaultConnection")));

services.Configure<FormOptions>(x =>
{
x.ValueLengthLimit = int.MaxValue;
Expand Down
18 changes: 11 additions & 7 deletions Library.Encyclopedia.API/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,21 @@
"App-Base-Url": "https://tools.library.pfw.edu/encyclopedia",
"AllowedHosts": "*",
"ConnectionStrings": {
"DefaultConnection": "Server=localhost;Database=Encyclopedia;User=root;Password=root"
//"DefaultConnection": "Server=localhost;Database=Encyclopedia;User=root;Password=RW_qh+-ta5hW*2s"
//"DefaultConnection": "Server=localhost;Database=Encyclopedia;User=root;Password=root"
"DefaultConnection": "Server=localhost;Database=Encyclopedia;User=root;Password=RW_qh+-ta5hW*2s"
},
"ApiKey": "5929b003-8895-4fb3-bbb0-2eb101c48f66",
"FTPSettings": {
"FileAdapterSettings": {
// linux or windows
"type": "linux",

// For Linux
//"url": "10.161.100.82",
//"username": "mazus01",
//"password": ""
"url": "10.161.100.82",
"username": "serviceuser",
"password": "P!ssword+007",
"basePath": "Applications/PFW Encyclopedia Files"

// For Windows
"filePath": "O:\\Library\\Departments\\Applications\\PFW Encyclopedia Files\\"
//"filePath": "C:\\inetpub\\wwwroot\\PFW Encyclopedia Files\\"
}
}
86 changes: 86 additions & 0 deletions Library.Encyclopedia.DataAccess/FileAccess/LinuxFileSaveAdapter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using Library.Encyclopedia.Entity.Interfaces;
using Microsoft.AspNetCore.Http;
using Renci.SshNet;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Library.Encyclopedia.DataAccess.FileAccess
{
public class LinuxFileSaveAdapter : IFilesAdapter, IDisposable
{
private readonly string url;
private readonly string basePath;
private readonly NetworkCredential networkCredential;
private SftpClient client;

public LinuxFileSaveAdapter(string url, string userName, string password, string basePath)
{
this.url = url;
this.basePath = basePath;
this.networkCredential = new NetworkCredential(userName, password);

client = new SftpClient(url, 22, networkCredential.UserName, networkCredential.Password);
client.Connect();
}

public void DeleteFile(string id, string name)
{
var completeUrl = $@"/home/{networkCredential.UserName}/" + basePath + '/' + id.ToString() + '/' + name;


if (client.Exists(completeUrl))
client.DeleteFile(completeUrl);
}

public void DeleteFiles(string id)
{
var completeUrl = $@"/home/{networkCredential.UserName}/" + basePath + '/' + id.ToString();

if (client.Exists(completeUrl))
{
IEnumerable<Renci.SshNet.Sftp.SftpFile> files = client.ListDirectory(completeUrl);
foreach (var file in files)
{
if (file.Name != "." && file.Name != "..")
{
client.DeleteFile(file.FullName);
}
}

client.DeleteDirectory(completeUrl);
}
}

public void Dispose()
{
this.client.Disconnect();
this.client.Dispose();
}

public Stream DownloadFile(string id, string name)
{
var completeUrl = $@"/home/{networkCredential.UserName}/" + basePath + '/' + id.ToString() + '/' + name;
return client.OpenRead(completeUrl);
}

public Task<bool> UploadFile(Guid id, IFormFile file)
{
var completeUrl = $@"/home/{networkCredential.UserName}/" + basePath + '/' + id.ToString() + '/' + file.FileName;

if (!client.Exists($@"/home/{networkCredential.UserName}/" + basePath + '/' + id.ToString()))
client.CreateDirectory($@"/home/{networkCredential.UserName}/" + basePath + '/' + id.ToString());

if (client.Exists(completeUrl))
client.DeleteFile(completeUrl);

client.Create(completeUrl);
client.UploadFile(file.OpenReadStream(), completeUrl, true);

return Task.FromResult(true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void DeleteFiles(string id)
Directory.Delete(Path.Combine(networkPath, id));
}

public FileStream DownloadFile(string id, string name)
public Stream DownloadFile(string id, string name)
{
return File.OpenRead(Path.Combine(networkPath, id, name));
}
Expand Down
2 changes: 1 addition & 1 deletion Library.Encyclopedia.Entity/Interfaces/IFilesAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Library.Encyclopedia.Entity.Interfaces
public interface IFilesAdapter
{
public Task<bool> UploadFile(Guid id, IFormFile file);
FileStream DownloadFile(string id, string name);
Stream DownloadFile(string id, string name);
public void DeleteFiles(string id);
public void DeleteFile(string id, string name);
}
Expand Down

0 comments on commit b9b2f42

Please sign in to comment.