Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

don't limit controller with artifical guards #17

Merged
merged 1 commit into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 18 additions & 75 deletions GoFileSharp/GoFileSharp/Controllers/GoFileController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,72 +20,17 @@
public class GoFileController
{
private readonly string? _token;
private readonly AccountType _accountType = AccountType.Guest;
private HttpClient _client = new HttpClient();

/// <summary>
/// A GoFile API Controller
/// </summary>
/// <param name="token">The GoFile API token to use with this controller</param>
/// <param name="timeout">The HttpClient timeout. Defaults to 1 hour</param>
protected GoFileController(string? token = null, TimeSpan? timeout = null)
public GoFileController(string? token = null, TimeSpan? timeout = null)
{
_client.Timeout = timeout ?? TimeSpan.FromHours(1);

if (string.IsNullOrWhiteSpace(token))
{
return;
}

_token = token;

var accountIdResponse = GetAccountId().GetAwaiter().GetResult();

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

var accountResponse = GetAccountDetails(accountIdResponse.Data.Id)
.GetAwaiter().GetResult();

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

_accountType = accountResponse.Data.Tier;
}

/// <summary>
/// Initialize a new GoFile API Controller
/// </summary>
/// <param name="token">The GoFile API token to use with requests</param>
/// <param name="timeout">The timeout to use for the http client</param>
/// <returns>A new <see cref="GoFileController"/> that is ready to use</returns>
public static GoFileController Init(string? token = null, TimeSpan? timeout = null)
{
return new GoFileController(token, timeout);
}

private bool CheckAccountAllowed(AccountType minRequiredAccountType, out string message)
{
message = "ok";

if (minRequiredAccountType == AccountType.Guest)
{
return true;
}

if (_accountType >= minRequiredAccountType && !string.IsNullOrWhiteSpace(_token))
{
return true;
}

message =
$"A {minRequiredAccountType} account or higher is required for this call. Currently using a '{_accountType}' account";

return false;
_client.Timeout = timeout ?? TimeSpan.FromHours(1);
}

private async Task<GoFileResponse<T>> DeserializeResponse<T>(HttpResponseMessage response) where T : class
Expand Down Expand Up @@ -145,9 +90,9 @@
/// <remarks>This call requires a GoFile Premium account or higher</remarks>
public async Task<GoFileResponse<IContent>> GetContentAsync(string contentId, bool noCache = false, string? passwordHash = null)
{
if (!CheckAccountAllowed(AccountType.Premium, out string message))
if (_token == null)
{
return new GoFileResponse<IContent>() { Status = message};
return new GoFileResponse<IContent>() { Status = "A token is required for this call"};
}

var contentRequest = GoFileRequest.GetContents(_token, contentId, noCache, passwordHash);
Expand Down Expand Up @@ -189,7 +134,7 @@
/// <param name="progress">A progress object to use to track download progress</param>
/// <returns>Returns a GoFile response</returns>
/// <remarks>This response is not deserialized from GoFile and is only used for consistency</remarks>
public async Task<GoFileResponse<object>> DownloadFileAsync(string directDownloadLink, FileInfo destinationFile, bool overwrite = false, IProgress<double> progress = null)

Check warning on line 137 in GoFileSharp/GoFileSharp/Controllers/GoFileController.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.
{
try
{
Expand Down Expand Up @@ -225,7 +170,7 @@
/// <param name="folderId">The folder ID to upload into</param>
/// <returns>The response from GoFile including the uploaded file info</returns>
/// <remarks>This call does not require a GoFile account to use: Accessible as guest</remarks>
public async Task<GoFileResponse<UploadInfo>> UploadFileAsync(FileInfo file, ServerZone zone, IProgress<double> progress = null, string folderId = null)

Check warning on line 173 in GoFileSharp/GoFileSharp/Controllers/GoFileController.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.

Check warning on line 173 in GoFileSharp/GoFileSharp/Controllers/GoFileController.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.
{
file.Refresh();

Expand Down Expand Up @@ -297,7 +242,6 @@
/// <remarks>This call requires a GoFile Standard account or higher</remarks>
public async Task<GoFileResponse<AccountId>> GetAccountId()
{
// we need the id to get the account details / tier, so only checking to make sure a token is provided here
if (_token == null)
{
return new GoFileResponse<AccountId>() { Status = "A token is required for this call" };
Expand All @@ -323,7 +267,6 @@
/// <remarks>This call requires a GoFile Standard account or higher</remarks>
public async Task<GoFileResponse<AccountDetails>> GetAccountDetails(string? accountId = null)
{
// we need to get the account details / tier, so only checking to make sure a token is provided here
if (_token == null)
{
return new GoFileResponse<AccountDetails>() { Status = "A token is required for this call" };
Expand Down Expand Up @@ -362,9 +305,9 @@
/// <remarks>This call requires a GoFile Standard account or higher</remarks>
public async Task<GoFileResponse<FolderData>> CreateFolder(string parentFolderId, string? folderName = null)
{
if (!CheckAccountAllowed(AccountType.Standard, out string message))
if (_token == null)
{
return new GoFileResponse<FolderData>() { Status = message };
return new GoFileResponse<FolderData>() { Status = "A token is required for this call"};
}

var createFolderRequest = GoFileRequest.CreateFolder(_token, parentFolderId, folderName);
Expand All @@ -388,9 +331,9 @@
/// <remarks>This call requires a GoFile Premium account or higher.</remarks>
public async Task<GoFileResponse<object>> CopyContent(string[] contentIds, string destinationFolderId)
{
if (!CheckAccountAllowed(AccountType.Premium, out string message))
if (_token == null)
{
return new GoFileResponse<object>() { Status = message };
return new GoFileResponse<object>() { Status = "A token is required for this call"};
}

var copyRequest = GoFileRequest.CopyContents(_token, contentIds, destinationFolderId);
Expand All @@ -413,9 +356,9 @@
/// <remarks>This call requires a GoFile Standard account or higher</remarks>
public async Task<GoFileResponse<Dictionary<string, DeleteInfo>>> DeleteContent(string[] contentIds)
{
if (!CheckAccountAllowed(AccountType.Standard, out string message))
if (_token == null)
{
return new GoFileResponse<Dictionary<string, DeleteInfo>>() { Status = message };
return new GoFileResponse<Dictionary<string, DeleteInfo>>() { Status = "A token is required for this call"};
}

var deleteRequest = GoFileRequest.DeleteContent(_token, contentIds);
Expand All @@ -439,9 +382,9 @@
/// <remarks>This call requires a GoFile Standard account or higher</remarks>
public async Task<GoFileResponse<object>> UpdateContent(string contentId, IContentOption option)
{
if (!CheckAccountAllowed(AccountType.Standard, out string message))
if (_token == null)
{
return new GoFileResponse<object>() { Status = message };
return new GoFileResponse<object>() { Status = "A token is required for this call"};
}

var setOptionRequset = GoFileRequest.UpdateContent(_token, contentId, option.OptionName, option.Value);
Expand Down Expand Up @@ -476,9 +419,9 @@
/// <remarks>This call requires a GoFile Premium account or higher</remarks>
public async Task<GoFileResponse<DirectLink>> AddDirectLink(string contentId, DirectLinkOptions? options = null)
{
if (!CheckAccountAllowed(AccountType.Premium, out string message))
if (_token == null)
{
return new GoFileResponse<DirectLink>() { Status = message };
return new GoFileResponse<DirectLink>() { Status = "A token is required for this call"};
}

var addLinkRequest = GoFileRequest.CreateDirectLink(_token, contentId, options?.ExpireTime?.ToUnixTimeSeconds(),
Expand All @@ -504,9 +447,9 @@
/// <remarks>This call requires a GoFile Premium account or higher</remarks>
public async Task<GoFileResponse<DirectLink>> UpdateDirectLink(string contentId, string directLinkId, DirectLinkOptions options)
{
if (!CheckAccountAllowed(AccountType.Premium, out string message))
if (_token == null)
{
return new GoFileResponse<DirectLink>() { Status = message };
return new GoFileResponse<DirectLink>() { Status = "A token is required for this call"};
}

var updateLinkRequest = GoFileRequest.UpdateDirectLink(_token, contentId, directLinkId, options.ExpireTime?.ToUnixTimeSeconds(),
Expand All @@ -532,9 +475,9 @@
/// <remarks>This call requires a GoFile Premium account or higher</remarks>
public async Task<GoFileResponse<object>> RemoveDirectLink(string contentId, string directLinkId)
{
if (CheckAccountAllowed(AccountType.Premium, out string message))
if (_token == null)
{
return new GoFileResponse<object>() { Status = message };
return new GoFileResponse<object>() { Status = "A token is required for this call"};
}

var deleteLinkRequest = GoFileRequest.DeleteDirectLink(_token, contentId, directLinkId);
Expand Down
2 changes: 1 addition & 1 deletion GoFileSharp/GoFileSharp/GoFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
_options = options;
}

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

/// <summary>
Expand Down Expand Up @@ -96,7 +96,7 @@
/// <param name="folderId">The id of the folder to upload the file into</param>
/// <returns>Returns the uploaded file</returns>
/// <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.
{
var uploadResponse = await _api.UploadFileAsync(file, _options.PreferredZone, progress, folderId);

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.1.0</AssemblyVersion>
<FileVersion>1.1.0</FileVersion>
<Version>1.1.0</Version>
<AssemblyVersion>1.1.1</AssemblyVersion>
<FileVersion>1.1.1</FileVersion>
<Version>1.1.1</Version>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageIcon>gofilesharp_icon.png</PackageIcon>
<PackageLicenseExpression>GPL-3.0-only</PackageLicenseExpression>
Expand Down
Loading