using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Threading.Tasks; using Oqtane.Documentation; using Oqtane.Models; using Oqtane.Shared; namespace Oqtane.Services { /// /// Service to get / create / upload / download files. /// public interface IFileService { /// /// Get all s in the specified Folder /// /// The folder ID /// Task> GetFilesAsync(int folderId); /// /// Get all s in the specified folder. /// /// /// The folder path relative to where the files are stored. /// TODO: todoc verify exactly from where the folder path must start /// /// Task> GetFilesAsync(string folder); /// /// Get one /// /// /// Task GetFileAsync(int fileId); /// /// Get a based on the and file name. /// /// Reference to the /// name of the file /// /// Task GetFileAsync(int folderId, string name); /// /// Add / store a record. /// This does not contain the file contents. /// /// /// Task AddFileAsync(File file); /// /// Update a record. /// Use this for rename a file or change some attributes. /// This does not contain the file contents. /// /// /// Task UpdateFileAsync(File file); /// /// Delete a /// /// /// Task DeleteFileAsync(int fileId); /// /// Upload a file from a URL to a /// /// /// /// /// Task UploadFileAsync(string url, int folderId, string name); /// /// Get / download a file (the body). /// /// Reference to a /// The bytes of the file Task DownloadFileAsync(int fileId); /// /// Retrieve a list of files from a and /// /// Reference to the /// Path of the folder /// TODO: todoc verify exactly from where the folder path must start /// /// Task> GetFilesAsync(int siteId, string folderPath); /// /// Unzips the contents of a zip file /// /// Reference to the /// /// Task UnzipFileAsync(int fileId); } [PrivateApi("Don't show in the documentation, as everything should use the Interface")] public class FileService : ServiceBase, IFileService { public FileService(HttpClient http, SiteState siteState) : base(http, siteState) { } private string Apiurl => CreateApiUrl("File"); public async Task> GetFilesAsync(int folderId) { return await GetFilesAsync(folderId.ToString()); } public async Task> GetFilesAsync(string folder) { List files = await GetJsonAsync>($"{Apiurl}?folder={folder}"); return files.OrderBy(item => item.Name).ToList(); } public async Task> GetFilesAsync(int siteId, string folderPath) { if (!(string.IsNullOrEmpty(folderPath) || folderPath.EndsWith(System.IO.Path.DirectorySeparatorChar) || folderPath.EndsWith(System.IO.Path.AltDirectorySeparatorChar))) { folderPath = Utilities.UrlCombine(folderPath) + "/"; } var path = WebUtility.UrlEncode(folderPath); List files = await GetJsonAsync>($"{Apiurl}/{siteId}/{path}"); return files?.OrderBy(item => item.Name).ToList(); } public async Task GetFileAsync(int fileId) { return await GetJsonAsync($"{Apiurl}/{fileId}"); } public async Task GetFileAsync(int folderId, string name) { return await GetJsonAsync($"{Apiurl}/name/{name}/{folderId}"); } public async Task AddFileAsync(File file) { return await PostJsonAsync(Apiurl, file); } public async Task UpdateFileAsync(File file) { return await PutJsonAsync($"{Apiurl}/{file.FileId}", file); } public async Task DeleteFileAsync(int fileId) { await DeleteAsync($"{Apiurl}/{fileId}"); } public async Task UploadFileAsync(string url, int folderId, string name) { return await GetJsonAsync($"{Apiurl}/upload?url={WebUtility.UrlEncode(url)}&folderid={folderId}&name={name}"); } public async Task DownloadFileAsync(int fileId) { return await GetByteArrayAsync($"{Apiurl}/download/{fileId}"); } public async Task UnzipFileAsync(int fileId) { await PutAsync($"{Apiurl}/unzip/{fileId}"); } } }