-
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.
Added upload and download functionality
- Loading branch information
Showing
14 changed files
with
345 additions
and
159 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| using Library.Encyclopedia.Entity.Interfaces; | ||
| using Library.Encyclopedia.Entity.Models; | ||
| using Microsoft.AspNetCore.Http; | ||
| using Renci.SshNet; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.Net; | ||
| using System.Text; | ||
|
|
||
| namespace Library.Encyclopedia.DataAccess.FileAccess | ||
| { | ||
| public class FTPAdapter : IFilesAdapter | ||
| { | ||
| private readonly string url; | ||
| private readonly NetworkCredential networkCredential; | ||
|
|
||
| public FTPAdapter(string url, NetworkCredential networkCredential) | ||
| { | ||
| this.url = url; | ||
| this.networkCredential = networkCredential; | ||
| } | ||
|
|
||
| public IEnumerable<bool> DownloadFiles(IEnumerable<Files> files, string directory) | ||
| { | ||
| List<bool> responses = new List<bool>(); | ||
| var customUrl = $@"/home/{networkCredential.UserName}"; | ||
|
|
||
| using (SftpClient client = new SftpClient(url, 22, networkCredential.UserName, networkCredential.Password)) | ||
| { | ||
| client.Connect(); | ||
| foreach (var file in files) | ||
| { | ||
| using (var stream = File.OpenWrite(Path.Combine(directory, file.FileName))) | ||
| { | ||
| client.DownloadFile(customUrl + file.FilePath, stream); | ||
| responses.Add(true); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return responses; | ||
| } | ||
|
|
||
| public IEnumerable<bool> UploadFiles(Guid id, List<IFormFile> files) | ||
| { | ||
| List<bool> responses = new List<bool>(); | ||
| var customUrl = $@"/home/{networkCredential.UserName}/encyclopedia-media/{id}/"; | ||
|
|
||
| using (SftpClient client = new SftpClient(url, 22, networkCredential.UserName, networkCredential.Password)) | ||
| { | ||
| client.Connect(); | ||
| if (!client.Exists(customUrl)) | ||
| client.CreateDirectory(customUrl); | ||
|
|
||
| foreach (var file in files) | ||
| { | ||
| if (client.Exists(customUrl + file.FileName)) | ||
| client.DeleteFile(customUrl + file.FileName); | ||
|
|
||
| client.UploadFile(file.OpenReadStream(), customUrl + file.FileName); | ||
| responses.Add(true); | ||
| } | ||
| } | ||
|
|
||
| return responses; | ||
| } | ||
| } | ||
| } |
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
Oops, something went wrong.