Skip to content

Commit

Permalink
Merge pull request #16 from waffle-lord/impr/account-based-access
Browse files Browse the repository at this point in the history
Impr/account based access
  • Loading branch information
waffle-lord authored Apr 16, 2024
2 parents 8ef066e + 36b2338 commit e90c3d4
Show file tree
Hide file tree
Showing 10 changed files with 237 additions and 93 deletions.
198 changes: 154 additions & 44 deletions GoFileSharp/GoFileSharp/Controllers/GoFileController.cs

Large diffs are not rendered by default.

31 changes: 18 additions & 13 deletions GoFileSharp/GoFileSharp/GoFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,28 @@ namespace GoFileSharp
public class GoFile
{
private readonly GoFileOptions _options = new GoFileOptions();
private readonly GoFileController _api = new GoFileController();
private readonly GoFileController _api;

public GoFile(GoFileOptions? options = null)
{
if (options == null)
return;
if (options != null)
{
_options = options;
}

_options = options;
_api = GoFileController.Init(_options.ApiToken);
}

/// <summary>
/// Get <see cref="IContent"/> from an ID
/// </summary>
/// <param name="contentId">The content ID to try and get</param>
/// <param name="noCache">Wheather or not to use GoFile cache with this request</param>
/// <param name="noCache">Whether or not to use GoFile cache with this request</param>
/// <param name="passwordHash">The SHA256 hash of the password to use for password protected content</param>
/// <returns>Returns content of the id</returns>
private async Task<IContent?> GetContentAsync(string contentId, bool noCache = false, string? passwordHash = null)
{
var response = await _api.GetContentAsync(contentId, _options.ApiToken, noCache, passwordHash);
var response = await _api.GetContentAsync(contentId, noCache, passwordHash);

if(response is { IsOK: true, Data: { } data })
{
Expand All @@ -53,9 +55,10 @@ public GoFile(GoFileOptions? options = null)
/// Get a folder object from an ID
/// </summary>
/// <param name="contentId"></param>
/// <param name="noCache">Wheather or not to use GoFile cache with this request</param>
/// <param name="noCache">Whether or not to use GoFile cache with this request</param>
/// <param name="passwordHash">The SHA256 hash of the password to use for password protected content</param>
/// <returns></returns>
/// <remarks>This call requires a GoFile Premium account or higher</remarks>
public async Task<GoFileFolder?> GetFolderAsync(string contentId, bool noCache = false, string? passwordHash = null)
{
var folder = await GetContentAsync(contentId, noCache, passwordHash);
Expand Down Expand Up @@ -86,32 +89,33 @@ public GoFile(GoFileOptions? options = null)
// }

/// <summary>
/// Upload a file to Gofile
/// Upload a file to GoFile. If the preferred zone option was set, the upload will use a server in that zone
/// </summary>
/// <param name="file">The file to upload</param>
/// <param name="progress">The progress object to use with the upload for progress updates</param>
/// <param name="folderId">The id of the folder to upload the file into</param>
/// <returns>Returns the uploaded file</returns>
/// <remarks>If the preferred zone option was set, the upload will use a server in that zone</remarks>
/// <remarks>This call does not require a GoFile account to use: Accessible as guest</remarks>
public async Task<GoFileFile?> UploadFileAsync(FileInfo file, IProgress<double> progress = null, string folderId = null)

Check warning on line 99 in GoFileSharp/GoFileSharp/GoFile.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.

Check warning on line 99 in GoFileSharp/GoFileSharp/GoFile.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.

Check warning on line 99 in GoFileSharp/GoFileSharp/GoFile.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.

Check warning on line 99 in GoFileSharp/GoFileSharp/GoFile.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.
{
var uploadResponse = await _api.UploadFileAsync(file, _options.PreferredZone, _options.ApiToken, progress, folderId);
var uploadResponse = await _api.UploadFileAsync(file, _options.PreferredZone, progress, folderId);

if(!uploadResponse.IsOK || uploadResponse.Data == null)
{
return null;
}

return await GoFileHelper.TryGetUplaodedFile(uploadResponse.Data, _options, _api);
return await GoFileHelper.TryGetUplaodedFile(uploadResponse.Data, _api);
}

/// <summary>
/// Get your account details
/// </summary>
/// <returns>Returns your account details</returns>
/// <remarks>This call requires a GoFile Standard account or higher</remarks>
public async Task<AccountDetails?> GetMyAccountAsync()
{
var accountResponse = await _api.GetAccountDetails(_options.ApiToken);
var accountResponse = await _api.GetAccountDetails();

return accountResponse.Data;
}
Expand All @@ -120,9 +124,10 @@ public GoFile(GoFileOptions? options = null)
/// Get the account's root folder
/// </summary>
/// <returns>Returns the root folder</returns>
/// <remarks>This call requires a GoFile Premium account or higher</remarks>
public async Task<GoFileFolder?> GetMyRootFolderAsync()
{
var accountDetailsResponse = await _api.GetAccountDetails(_options.ApiToken);
var accountDetailsResponse = await _api.GetAccountDetails();

if(!accountDetailsResponse.IsOK || accountDetailsResponse.Data == null)
{
Expand Down
6 changes: 3 additions & 3 deletions GoFileSharp/GoFileSharp/GoFileSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
<RepositoryUrl>https://github.com/waffle-lord/GoFileSharp</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>gofilesharp gofile</PackageTags>
<AssemblyVersion>1.0.4</AssemblyVersion>
<FileVersion>1.0.4</FileVersion>
<Version>1.0.4</Version>
<AssemblyVersion>1.1.0</AssemblyVersion>
<FileVersion>1.1.0</FileVersion>
<Version>1.1.0</Version>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageIcon>gofilesharp_icon.png</PackageIcon>
<PackageLicenseExpression>GPL-3.0-only</PackageLicenseExpression>
Expand Down
2 changes: 1 addition & 1 deletion GoFileSharp/GoFileSharp/Model/GoFileData/AccountDetails.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class AccountDetails

public string Email { get; set; }

public string Tier { get; set; }
public AccountType Tier { get; set; }

public string RootFolder { get; set; }

Expand Down
9 changes: 9 additions & 0 deletions GoFileSharp/GoFileSharp/Model/GoFileData/AccountType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace GoFileSharp.Model.GoFileData
{
public enum AccountType
{
Guest = 0,
Standard = 1,
Premium = 2
}
}
16 changes: 7 additions & 9 deletions GoFileSharp/GoFileSharp/Model/GoFileData/Wrappers/GoFileFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,16 @@ namespace GoFileSharp.Model.GoFileData.Wrappers
/// <remarks>The name is stupid, I know ...</remarks>
public class GoFileFile : FileData
{
private readonly GoFileOptions _options;
private readonly GoFileController _api;

public GoFileFile(FileData content, GoFileOptions options, GoFileController controller) : base(content)
public GoFileFile(FileData content, GoFileController controller) : base(content)
{
_options = options;
_api = controller;
}

private async Task<bool> SetOptionAndRefresh(IContentOption option)
{
var status = await _api.UpdateContent(_options.ApiToken, Id, option);
var status = await _api.UpdateContent(Id, option);

if (status.IsOK)
await RefreshAsync();
Expand All @@ -41,7 +39,7 @@ private async Task<bool> SetOptionAndRefresh(IContentOption option)
/// <remarks>Automatic refreshes, like when using a Set method (SetNameAsync for example) will not refresh if a password is set. You will need to call this manually with the password hash</remarks>
public async Task<bool> RefreshAsync(string? parentFolderPasswordHash = null)
{
var parent = await _api.GetContentAsync(ParentFolderId, _options.ApiToken, true, parentFolderPasswordHash);
var parent = await _api.GetContentAsync(ParentFolderId, true, parentFolderPasswordHash);

if (!parent.IsOK || parent.Data == null)
return false;
Expand Down Expand Up @@ -106,7 +104,7 @@ public async Task<bool> CopyToAsync(GoFileFolder destinationFolder)
/// <returns>Returns true if the file was deleted, otherwise false</returns>
public async Task<Dictionary<string, DeleteInfo>> DeleteAsync()
{
var result = await _api.DeleteContent(_options.ApiToken, new[] { this.Id });
var result = await _api.DeleteContent(new[] { this.Id });

return result.Data ?? new Dictionary<string, DeleteInfo>();
}
Expand All @@ -129,7 +127,7 @@ public async Task<bool> SetNameAsync(string newName) =>

private async Task<DirectLink?> AddDirectLinkAsync(DirectLinkOptions? options = null)
{
var response = await _api.AddDirectLink(_options.ApiToken, Id, options);
var response = await _api.AddDirectLink(Id, options);

if (response.IsOK)
await RefreshAsync();
Expand All @@ -148,7 +146,7 @@ public async Task<bool> SetNameAsync(string newName) =>

private async Task<DirectLink?> UpdateDirectLinkAsync(string directLinkId, DirectLinkOptions options)
{
var response = await _api.UpdateDirectLink(_options.ApiToken, Id, directLinkId, options);
var response = await _api.UpdateDirectLink(Id, directLinkId, options);

if (response.IsOK)
await RefreshAsync();
Expand All @@ -166,7 +164,7 @@ public async Task<bool> RemoveDirectLinkAsync(DirectLink directLink)

private async Task<bool> RemoveDirectLinkAsync(string directLinkId)
{
var response = await _api.RemoveDirectLink(_options.ApiToken, Id, directLinkId);
var response = await _api.RemoveDirectLink(Id, directLinkId);

if (response.IsOK)
await RefreshAsync();
Expand Down
36 changes: 18 additions & 18 deletions GoFileSharp/GoFileSharp/Model/GoFileData/Wrappers/GoFileFolder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
namespace GoFileSharp.Model.GoFileData.Wrappers
{
/// <summary>
/// A wrapper class for the GoFile <see cref="ContentInfo"/>
/// A wrapper class for the GoFile <see cref="FolderData"/>
/// </summary>
public class GoFileFolder : FolderData
{
private readonly GoFileController _api;
private readonly GoFileOptions _options;
private GoFileController _api;

public GoFileFolder(FolderData content, GoFileOptions options, GoFileController controller) : base(content)
{
Expand All @@ -25,7 +25,7 @@ public GoFileFolder(FolderData content, GoFileOptions options, GoFileController

private async Task<bool> SetOptionAndRefresh(IContentOption option)
{
var status = await _api.UpdateContent(_options.ApiToken, Id, option);
var status = await _api.UpdateContent(Id, option);

if (status.IsOK)
await RefreshAsync();
Expand All @@ -41,7 +41,7 @@ private async Task<bool> SetOptionAndRefresh(IContentOption option)
/// <remarks>Automatic refreshes, like when using a Set method (SetNameAsync for example) will not refresh if a password is set. You will need to call this manually with the password hash</remarks>
public async Task<bool> RefreshAsync(string? passwordHash = null)
{
var thisFolder = await _api.GetContentAsync(Id, _options.ApiToken, true, passwordHash);
var thisFolder = await _api.GetContentAsync(Id, true, passwordHash);

if(!thisFolder.IsOK || thisFolder.Data == null)
return false;
Expand All @@ -62,7 +62,7 @@ public async Task<bool> RefreshAsync(string? passwordHash = null)
/// <returns>Returns the newly created folder as a <see cref="GoFileFolder"/> object or null</returns>
public async Task<GoFileFolder?> CreateFolderAsync(string folderName)
{
var createFolderResponse = await _api.CreateFolder(_options.ApiToken, Id, folderName);
var createFolderResponse = await _api.CreateFolder(Id, folderName);

if(!createFolderResponse.IsOK || createFolderResponse.Data == null)
{
Expand All @@ -75,15 +75,15 @@ public async Task<bool> RefreshAsync(string? passwordHash = null)
/// <summary>
/// Find a file by name
/// </summary>
/// <param name="Name">The name of the file</param>
/// <param name="fileName">The name of the file</param>
/// <returns>Returns the file as a <see cref="GoFileFile"/> object or null</returns>
public GoFileFile? FindFile(string Name)
public GoFileFile? FindFile(string fileName)
{
var fileContent = Children.SingleOrDefault(x => x.Name == Name);
var fileContent = Children.SingleOrDefault(x => x.Name == fileName);

if(fileContent is FileData file)
{
return new GoFileFile(file, _options, _api);
return new GoFileFile(file, _api);
}

return null;
Expand All @@ -100,7 +100,7 @@ public async Task<bool> RefreshAsync(string? passwordHash = null)

if(folderContent is FolderData folderData)
{
var folderResponse = await _api.GetContentAsync(folderData.Id, _options.ApiToken, true);
var folderResponse = await _api.GetContentAsync(folderData.Id, true);

if(folderResponse.IsOK && folderResponse.Data != null && folderResponse.Data is FolderData folder)
{
Expand All @@ -119,14 +119,14 @@ public async Task<bool> RefreshAsync(string? passwordHash = null)
/// <returns>Returns the uplaoded file as a <see cref="GoFileFile"/> object</returns>
public async Task<GoFileFile?> UploadIntoAsync(FileInfo file, IProgress<double> progress = null)

Check warning on line 120 in GoFileSharp/GoFileSharp/Model/GoFileData/Wrappers/GoFileFolder.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.

Check warning on line 120 in GoFileSharp/GoFileSharp/Model/GoFileData/Wrappers/GoFileFolder.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.
{
var response = await _api.UploadFileAsync(file, _options.PreferredZone, _options.ApiToken, progress, Id);
var response = await _api.UploadFileAsync(file, _options.PreferredZone, progress, Id);

if(!response.IsOK || response.Data == null)
{
return null;
}

return await GoFileHelper.TryGetUplaodedFile(response.Data, _options, _api);
return await GoFileHelper.TryGetUplaodedFile(response.Data, _api);
}

/// <summary>
Expand All @@ -136,7 +136,7 @@ public async Task<bool> RefreshAsync(string? passwordHash = null)
/// <exception cref="NotImplementedException"></exception>
public async Task<Dictionary<string, DeleteInfo>> DeleteAsync()
{
var response = await _api.DeleteContent(_options.ApiToken, new[] { Id });
var response = await _api.DeleteContent(new[] { Id });

return response.Data ?? new Dictionary<string, DeleteInfo>();
}
Expand All @@ -148,7 +148,7 @@ public async Task<Dictionary<string, DeleteInfo>> DeleteAsync()
/// <returns>Returns true if this folder was copied into the destination folder, otherwise false</returns>
public async Task<bool> CopyToAsync(GoFileFolder destinationFolder)
{
return await destinationFolder.CopyIntoAsync(new[] { this });
return await destinationFolder.CopyIntoAsync(new IContent[] { this });
}

/// <summary>
Expand All @@ -160,7 +160,7 @@ public async Task<bool> CopyIntoAsync(IContent[] content)
{
var contentIds = content.Select(x => x.Id).ToArray();

var copyResponse = await _api.CopyContent(_options.ApiToken, contentIds, Id);
var copyResponse = await _api.CopyContent(contentIds, Id);

return copyResponse.IsOK;
}
Expand Down Expand Up @@ -223,7 +223,7 @@ public async Task<bool> SetNameAsync(string newName)

private async Task<DirectLink?> AddDirectLinkAsync(DirectLinkOptions? options = null)
{
var response = await _api.AddDirectLink(_options.ApiToken, Id, options);
var response = await _api.AddDirectLink(Id, options);

if (response.IsOK)
await RefreshAsync();
Expand All @@ -242,7 +242,7 @@ public async Task<bool> SetNameAsync(string newName)

private async Task<DirectLink?> UpdateDirectLinkAsync(string directLinkId, DirectLinkOptions options)
{
var response = await _api.UpdateDirectLink(_options.ApiToken, Id, directLinkId, options);
var response = await _api.UpdateDirectLink(Id, directLinkId, options);

if (response.IsOK)
await RefreshAsync();
Expand All @@ -260,7 +260,7 @@ public async Task<bool> RemoveDirectLinkAsync(DirectLink directLink)

private async Task<bool> RemoveDirectLinkAsync(string directLinkId)
{
var response = await _api.RemoveDirectLink(_options.ApiToken, Id, directLinkId);
var response = await _api.RemoveDirectLink(Id, directLinkId);

if (response.IsOK)
await RefreshAsync();
Expand Down
6 changes: 3 additions & 3 deletions GoFileSharp/GoFileSharp/Model/GoFileHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace GoFileSharp.Model
{
public static class GoFileHelper
{
public static async Task<GoFileFile?> TryGetUplaodedFile(UploadInfo uploadInfo, GoFileOptions options, GoFileController api, string? passwordHash = null)
public static async Task<GoFileFile?> TryGetUplaodedFile(UploadInfo uploadInfo, GoFileController api, string? passwordHash = null)
{
// NOTE: This is mainly due to GoFile folder data not updating immediately after an upload :(
// up to 1 min to try and get uploaded file
Expand All @@ -21,7 +21,7 @@ public static class GoFileHelper

while (maxTries > 0)
{
var parentFolder = await api.GetContentAsync(uploadInfo.ParentFolderId, options.ApiToken, true, passwordHash);
var parentFolder = await api.GetContentAsync(uploadInfo.ParentFolderId, true, passwordHash);

if (!parentFolder.IsOK || parentFolder.Data == null)
return null;
Expand All @@ -44,7 +44,7 @@ public static class GoFileHelper

if (uploadedContent is FileData uploadedFile)
{
return new GoFileFile(uploadedFile, options, api);
return new GoFileFile(uploadedFile, api);
}

return null;
Expand Down
10 changes: 9 additions & 1 deletion GoFileSharp/Sandbox/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
// See https://aka.ms/new-console-template for more information

Console.WriteLine("blah");
using GoFileSharp;
using GoFileSharp.Model;

var gofile = new GoFile(new GoFileOptions
{
PreferredZone = ServerZone.NorthAmerica
});

Console.WriteLine("hello :)");
Loading

0 comments on commit e90c3d4

Please sign in to comment.