Skip to content

Commit

Permalink
feat: Add shared link header support in file and folder managers (#923)
Browse files Browse the repository at this point in the history
  • Loading branch information
arjankowski committed Sep 20, 2023
1 parent 071ddcd commit ffbfc72
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 8 deletions.
57 changes: 57 additions & 0 deletions Box.V2.Test.Integration/BoxFilesManagerIntegrationTest.cs
Expand Up @@ -56,6 +56,40 @@ public async Task DownloadAsync_ForUploadedFile_ShouldReturnSameFileAsTheUploade
Assert.AreEqual(base64DownloadedFile, base64UploadedFile);
}

[TestMethod]
public async Task DownloadAsync_ForFileWithSharedLink_ShouldReturnSameFileAsTheUploadedFile()
{
var folder = await CreateFolderAsAdmin();
var uploadedFile = await CreateSmallFileAsAdmin(folder.Id);

var password = "SuperSecret123";
var sharedLinkRequest = new BoxSharedLinkRequest
{
Access = BoxSharedLinkAccessType.open,
Password = password
};

var sharedLink = await AdminClient.FilesManager.CreateSharedLinkAsync(uploadedFile.Id, sharedLinkRequest);

var downloadedFile = await UserClient.FilesManager.DownloadAsync(uploadedFile.Id, sharedLink: sharedLink.SharedLink.Url, sharedLinkPassword: password);

Stream fileContents = new MemoryStream();
await downloadedFile.CopyToAsync(fileContents);
fileContents.Position = 0;

string base64DownloadedFile;
string base64UploadedFile;

base64DownloadedFile = Helper.GetSha1Hash(fileContents);

using (var fileStream = new FileStream(GetSmallFilePath(), FileMode.OpenOrCreate))
{
base64UploadedFile = Helper.GetSha1Hash(fileStream);
}

Assert.AreEqual(base64DownloadedFile, base64UploadedFile);
}

[TestMethod]
public async Task GetInformationAsync_ForCorrectFileId_ShouldReturnSameFileAsUploadedFile()
{
Expand All @@ -66,6 +100,29 @@ public async Task GetInformationAsync_ForCorrectFileId_ShouldReturnSameFileAsUpl
Assert.AreEqual(uploadedFile.Sha1, downloadedFile.Sha1);
}

[TestMethod]
public async Task GetInformationAsync_ForFileWithSharedLink_ShouldReturnSameFileAsUploadedFile()
{
var folder = await CreateFolderAsAdmin();
var uploadedFile = await CreateSmallFileAsAdmin(folder.Id);

var password = "SuperSecret123";
var sharedLinkRequest = new BoxSharedLinkRequest
{
Access = BoxSharedLinkAccessType.open,
Password = password
};

var sharedLink = await AdminClient.FilesManager.CreateSharedLinkAsync(uploadedFile.Id, sharedLinkRequest);

var sharedItems = await UserClient.SharedItemsManager.SharedItemsAsync(sharedLink.SharedLink.Url, password);

BoxFile file = await UserClient.FilesManager.GetInformationAsync(sharedItems.Id, sharedLink: sharedLink.SharedLink.Url, sharedLinkPassword: password);

Assert.AreEqual(file.Id, uploadedFile.Id);
Assert.AreEqual(file.Sha1, uploadedFile.Sha1);
}

[TestMethod]
public async Task GetDownloadUriAsync_ForCorrectFileId_ShouldReturnCorrectUrl()
{
Expand Down
20 changes: 20 additions & 0 deletions Box.V2.Test.Integration/BoxFolderManagerIntegrationTest.cs
Expand Up @@ -76,6 +76,26 @@ public async Task GetInformationAsync_ForExistingFolder_ShouldReturnInformationR
Assert.AreEqual(folder.Id, createdFolder.Id);
}

[TestMethod]
public async Task GetInformationAsync_ForFolderWithSharedLink_ShouldReturnInformationRelatedToThisFolder()
{
var createdFolder = await CreateFolderAsAdmin();

var password = "SuperSecret123";
var sharedLinkRequest = new BoxSharedLinkRequest
{
Access = BoxSharedLinkAccessType.open,
Password = password
};
var sharedLink = await AdminClient.FoldersManager.CreateSharedLinkAsync(createdFolder.Id, sharedLinkRequest);

var sharedItems = await UserClient.SharedItemsManager.SharedItemsAsync(sharedLink.SharedLink.Url, password);

BoxFolder folder = await UserClient.FoldersManager.GetInformationAsync(sharedItems.Id, sharedLink: sharedLink.SharedLink.Url, sharedLinkPassword: password);

Assert.AreEqual(folder.Id, createdFolder.Id);
}

[TestMethod]
public async Task CreateSharedLinkAsync_ForExistingFolder_ShouldCreateSharedLinkToThatFolder()
{
Expand Down
20 changes: 18 additions & 2 deletions Box.V2/Managers/BoxFilesManager.cs
Expand Up @@ -36,14 +36,22 @@ public BoxFilesManager(IBoxConfig config, IBoxService service, IBoxConverter con
/// </summary>
/// <param name="id">Id of the file.</param>
/// <param name="fields">Attribute(s) to include in the response.</param>
/// <param name="sharedLink">The shared link for this file</param>
/// <param name="sharedLinkPassword">The password for the shared link (if required)</param>
/// <returns>A full file object is returned if the ID is valid and if the user has access to the file.</returns>
public async Task<BoxFile> GetInformationAsync(string id, IEnumerable<string> fields = null)
public async Task<BoxFile> GetInformationAsync(string id, IEnumerable<string> fields = null, string sharedLink = null, string sharedLinkPassword = null)
{
id.ThrowIfNullOrWhiteSpace("id");

BoxRequest request = new BoxRequest(_config.FilesEndpointUri, id)
.Param(ParamFields, fields);

if (!string.IsNullOrEmpty(sharedLink))
{
var sharedLinkHeader = SharedLinkUtils.GetSharedLinkHeader(sharedLink, sharedLinkPassword);
request.Header(sharedLinkHeader.Item1, sharedLinkHeader.Item2);
}

IBoxResponse<BoxFile> response = await ToResponseAsync<BoxFile>(request).ConfigureAwait(false);

return response.ResponseObject;
Expand All @@ -57,8 +65,10 @@ public async Task<BoxFile> GetInformationAsync(string id, IEnumerable<string> fi
/// <param name="timeout">Optional timeout for response.</param>
/// <param name="startOffsetInBytes">Starting byte of the chunk to download.</param>
/// <param name="endOffsetInBytes">Ending byte of the chunk to download.</param>
/// <param name="sharedLink">The shared link for this file</param>
/// <param name="sharedLinkPassword">The password for the shared link (if required)</param>
/// <returns>Stream of the requested file.</returns>
public async Task<Stream> DownloadAsync(string id, string versionId = null, TimeSpan? timeout = null, long? startOffsetInBytes = null, long? endOffsetInBytes = null)
public async Task<Stream> DownloadAsync(string id, string versionId = null, TimeSpan? timeout = null, long? startOffsetInBytes = null, long? endOffsetInBytes = null, string sharedLink = null, string sharedLinkPassword = null)
{
id.ThrowIfNullOrWhiteSpace("id");

Expand All @@ -70,6 +80,12 @@ public async Task<Stream> DownloadAsync(string id, string versionId = null, Time
request = request.Header("Range", $"bytes={startOffsetInBytes}-{endOffsetInBytes}");
}

if (!string.IsNullOrEmpty(sharedLink))
{
var sharedLinkHeader = SharedLinkUtils.GetSharedLinkHeader(sharedLink, sharedLinkPassword);
request.Header(sharedLinkHeader.Item1, sharedLinkHeader.Item2);
}

IBoxResponse<Stream> response = await ToResponseAsync<Stream>(request).ConfigureAwait(false);
return response.ResponseObject;
}
Expand Down
12 changes: 10 additions & 2 deletions Box.V2/Managers/BoxFoldersManager.cs
Expand Up @@ -96,16 +96,24 @@ public async Task<BoxFolder> CreateAsync(BoxFolderRequest folderRequest, IEnumer
/// </summary>
/// <param name="id">The folder id</param>
/// <param name="fields">Attribute(s) to include in the response</param>
/// <returns>A full folder object is returned, including the most current information available about it.
/// <param name="sharedLink">The shared link for this folder</param>
/// <param name="sharedLinkPassword">The password for the shared link (if required)</param>
/// <returns>A full folder object is returned, including the most current information available about it.
/// An exception is thrown if the folder does not exist or if the user does not have access to it.</returns>
public async Task<BoxFolder> GetInformationAsync(string id, IEnumerable<string> fields = null)
public async Task<BoxFolder> GetInformationAsync(string id, IEnumerable<string> fields = null, string sharedLink = null, string sharedLinkPassword = null)
{
id.ThrowIfNullOrWhiteSpace("id");

BoxRequest request = new BoxRequest(_config.FoldersEndpointUri, id)
.Method(RequestMethod.Get)
.Param(ParamFields, fields);

if (!string.IsNullOrEmpty(sharedLink))
{
var sharedLinkHeader = SharedLinkUtils.GetSharedLinkHeader(sharedLink, sharedLinkPassword);
request.Header(sharedLinkHeader.Item1, sharedLinkHeader.Item2);
}

IBoxResponse<BoxFolder> response = await ToResponseAsync<BoxFolder>(request).ConfigureAwait(false);

return response.ResponseObject;
Expand Down
8 changes: 6 additions & 2 deletions Box.V2/Managers/IBoxFilesManager.cs
Expand Up @@ -19,8 +19,10 @@ public interface IBoxFilesManager
/// </summary>
/// <param name="id">Id of the file.</param>
/// <param name="fields">Attribute(s) to include in the response.</param>
/// <param name="sharedLink">The shared link for this file</param>
/// <param name="sharedLinkPassword">The password for the shared link (if required)</param>
/// <returns>A full file object is returned if the ID is valid and if the user has access to the file.</returns>
Task<BoxFile> GetInformationAsync(string id, IEnumerable<string> fields = null);
Task<BoxFile> GetInformationAsync(string id, IEnumerable<string> fields = null, string sharedLink = null, string sharedLinkPassword = null);

/// <summary>
/// Returns the stream of the requested file.
Expand All @@ -30,8 +32,10 @@ public interface IBoxFilesManager
/// <param name="timeout">Optional timeout for response.</param>
/// <param name="startOffsetInBytes">Starting byte of the chunk to download.</param>
/// <param name="endOffsetInBytes">Ending byte of the chunk to download.</param>
/// <param name="sharedLink">The shared link for this file</param>
/// <param name="sharedLinkPassword">The password for the shared link (if required)</param>
/// <returns>Stream of the requested file.</returns>
Task<Stream> DownloadAsync(string id, string versionId = null, TimeSpan? timeout = null, long? startOffsetInBytes = null, long? endOffsetInBytes = null);
Task<Stream> DownloadAsync(string id, string versionId = null, TimeSpan? timeout = null, long? startOffsetInBytes = null, long? endOffsetInBytes = null, string sharedLink = null, string sharedLinkPassword = null);

/// <summary>
/// Retrieves the temporary direct Uri to a file (valid for 15 minutes). This is typically used to send as a redirect to a browser to make the browser download the file directly from Box.
Expand Down
6 changes: 4 additions & 2 deletions Box.V2/Managers/IBoxFoldersManager.cs
Expand Up @@ -45,9 +45,11 @@ public interface IBoxFoldersManager
/// </summary>
/// <param name="id">The folder id</param>
/// <param name="fields">Attribute(s) to include in the response</param>
/// <returns>A full folder object is returned, including the most current information available about it.
/// <param name="sharedLink">The shared link for this folder</param>
/// <param name="sharedLinkPassword">The password for the shared link (if required)</param>
/// <returns>A full folder object is returned, including the most current information available about it.
/// An exception is thrown if the folder does not exist or if the user does not have access to it.</returns>
Task<BoxFolder> GetInformationAsync(string id, IEnumerable<string> fields = null);
Task<BoxFolder> GetInformationAsync(string id, IEnumerable<string> fields = null, string sharedLink = null, string sharedLinkPassword = null);

/// <summary>
/// Used to create a copy of a folder in another folder. The original version of the folder will not be altered.
Expand Down

0 comments on commit ffbfc72

Please sign in to comment.