using System; 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 { [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}"); } } }