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

feat: Add shared link support in file and folder managers #923

Merged
merged 1 commit into from
Sep 20, 2023
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
57 changes: 57 additions & 0 deletions Box.V2.Test.Integration/BoxFilesManagerIntegrationTest.cs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,22 @@
/// </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 @@
/// <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 @@
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 Expand Up @@ -1190,7 +1206,7 @@
/// and Single Page depending on whether the file type is supported by passing in the corresponding x-rep-hints header. This will generate a
/// representation with a template_url. We will then have to either replace the {+asset_path} with <page_number>.png for single page or empty string
/// for all other representation types.
/// </summary>

Check warning on line 1209 in Box.V2/Managers/BoxFilesManager.cs

View workflow job for this annotation

GitHub Actions / Build and Test - Core

XML comment has badly formed XML -- 'End tag 'summary' does not match the start tag 'page_number'.'

Check warning on line 1209 in Box.V2/Managers/BoxFilesManager.cs

View workflow job for this annotation

GitHub Actions / Integration tests

XML comment has badly formed XML -- 'End tag 'summary' does not match the start tag 'page_number'.'
/// <param name="boxRepresentationRequest">Object of type BoxRepresentationRequest that contains Box file id, x-rep-hints</param>
/// <returns>A full file object containing the updated representations template_url and state is returned.</returns>
/// </summary>
Expand Down Expand Up @@ -1227,7 +1243,7 @@
/// <param name="zipRequest">Object of type BoxZipRequest that contains name and items.</param>
/// <param name="output">The stream to where the zip file will be written.</param>
/// <returns>The status of the download.</returns>
/// </summary>

Check warning on line 1246 in Box.V2/Managers/BoxFilesManager.cs

View workflow job for this annotation

GitHub Actions / Build and Test - Core

XML comment has badly formed XML -- 'End tag was not expected at this location.'

Check warning on line 1246 in Box.V2/Managers/BoxFilesManager.cs

View workflow job for this annotation

GitHub Actions / Integration tests

XML comment has badly formed XML -- 'End tag was not expected at this location.'
public async Task<BoxZipDownloadStatus> DownloadZip(BoxZipRequest zipRequest, Stream output)
{
BoxZip createdZip = await CreateZip(zipRequest).ConfigureAwait(false);
Expand Down Expand Up @@ -1261,7 +1277,7 @@
/// and Single Page depending on whether the file type is supported by passing in the corresponding x-rep-hints header. This will generate a
/// representation with a template_url. We will then have to either replace the {+asset_path} with <page_number>.png for single page or empty string
/// for all other representation types.
/// </summary>

Check warning on line 1280 in Box.V2/Managers/BoxFilesManager.cs

View workflow job for this annotation

GitHub Actions / Build and Test - Core

XML comment has badly formed XML -- 'End tag 'summary' does not match the start tag 'page_number'.'

Check warning on line 1280 in Box.V2/Managers/BoxFilesManager.cs

View workflow job for this annotation

GitHub Actions / Integration tests

XML comment has badly formed XML -- 'End tag 'summary' does not match the start tag 'page_number'.'
/// <param name="boxRepresentationRequest">Object of type BoxRepresentationRequest that contains Box file id, x-rep-hints.</param>
/// <returns>A stream over the representation contents.</returns>
/// </summary>
Expand Down
12 changes: 10 additions & 2 deletions Box.V2/Managers/BoxFoldersManager.cs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
/// </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 @@
/// <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 Expand Up @@ -415,7 +419,7 @@
/// and Single Page depending on whether the file type is supported by passing in the corresponding x-rep-hints header. This will generate a
/// representation with a template_url. We will then have to either replace the {+asset_path} with <page_number>.png for single page or empty string
/// for all other representation types.
/// </summary>

Check warning on line 422 in Box.V2/Managers/IBoxFilesManager.cs

View workflow job for this annotation

GitHub Actions / Build and Test - Core

XML comment has badly formed XML -- 'End tag 'summary' does not match the start tag 'page_number'.'

Check warning on line 422 in Box.V2/Managers/IBoxFilesManager.cs

View workflow job for this annotation

GitHub Actions / Integration tests

XML comment has badly formed XML -- 'End tag 'summary' does not match the start tag 'page_number'.'
/// <param name="boxRepresentationRequest">Object of type BoxRepresentationRequest that contains Box file id, x-rep-hints</param>
/// <returns>A full file object containing the updated representations template_url and state is returned.</returns>
/// </summary>
Expand All @@ -426,7 +430,7 @@
/// and Single Page depending on whether the file type is supported by passing in the corresponding x-rep-hints header. This will generate a
/// representation with a template_url. We will then have to either replace the {+asset_path} with <page_number>.png for single page or empty string
/// for all other representation types.
/// </summary>

Check warning on line 433 in Box.V2/Managers/IBoxFilesManager.cs

View workflow job for this annotation

GitHub Actions / Build and Test - Core

XML comment has badly formed XML -- 'End tag 'summary' does not match the start tag 'page_number'.'

Check warning on line 433 in Box.V2/Managers/IBoxFilesManager.cs

View workflow job for this annotation

GitHub Actions / Integration tests

XML comment has badly formed XML -- 'End tag 'summary' does not match the start tag 'page_number'.'
/// <param name="boxRepresentationRequest">Object of type BoxRepresentationRequest that contains Box file id, x-rep-hints.</param>
/// <returns>A stream over the representation contents.</returns>
/// </summary>
Expand All @@ -438,7 +442,7 @@
/// <param name="zipRequest">Object of type BoxZipRequest that contains name and items.</param>
/// <param name="output">The stream to where the zip file will be written.</param>
/// <returns>The status of the download.</returns>
/// </summary>

Check warning on line 445 in Box.V2/Managers/IBoxFilesManager.cs

View workflow job for this annotation

GitHub Actions / Build and Test - Core

XML comment has badly formed XML -- 'End tag was not expected at this location.'

Check warning on line 445 in Box.V2/Managers/IBoxFilesManager.cs

View workflow job for this annotation

GitHub Actions / Integration tests

XML comment has badly formed XML -- 'End tag was not expected at this location.'
Task<BoxZipDownloadStatus> DownloadZip(BoxZipRequest zipRequest, Stream output);
}
}
6 changes: 4 additions & 2 deletions Box.V2/Managers/IBoxFoldersManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ Task<BoxCollection<BoxItem>> GetFolderItemsAsync(string id, int limit, int offse
/// </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
Loading