-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
132 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
Library.Encyclopedia.DataAccess/FileAccess/LinuxFileSaveAdapter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters