Skip to content

Commit ffbfc72

Browse files
authored
feat: Add shared link header support in file and folder managers (#923)
1 parent 071ddcd commit ffbfc72

File tree

6 files changed

+115
-8
lines changed

6 files changed

+115
-8
lines changed

Box.V2.Test.Integration/BoxFilesManagerIntegrationTest.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,40 @@ public async Task DownloadAsync_ForUploadedFile_ShouldReturnSameFileAsTheUploade
5656
Assert.AreEqual(base64DownloadedFile, base64UploadedFile);
5757
}
5858

59+
[TestMethod]
60+
public async Task DownloadAsync_ForFileWithSharedLink_ShouldReturnSameFileAsTheUploadedFile()
61+
{
62+
var folder = await CreateFolderAsAdmin();
63+
var uploadedFile = await CreateSmallFileAsAdmin(folder.Id);
64+
65+
var password = "SuperSecret123";
66+
var sharedLinkRequest = new BoxSharedLinkRequest
67+
{
68+
Access = BoxSharedLinkAccessType.open,
69+
Password = password
70+
};
71+
72+
var sharedLink = await AdminClient.FilesManager.CreateSharedLinkAsync(uploadedFile.Id, sharedLinkRequest);
73+
74+
var downloadedFile = await UserClient.FilesManager.DownloadAsync(uploadedFile.Id, sharedLink: sharedLink.SharedLink.Url, sharedLinkPassword: password);
75+
76+
Stream fileContents = new MemoryStream();
77+
await downloadedFile.CopyToAsync(fileContents);
78+
fileContents.Position = 0;
79+
80+
string base64DownloadedFile;
81+
string base64UploadedFile;
82+
83+
base64DownloadedFile = Helper.GetSha1Hash(fileContents);
84+
85+
using (var fileStream = new FileStream(GetSmallFilePath(), FileMode.OpenOrCreate))
86+
{
87+
base64UploadedFile = Helper.GetSha1Hash(fileStream);
88+
}
89+
90+
Assert.AreEqual(base64DownloadedFile, base64UploadedFile);
91+
}
92+
5993
[TestMethod]
6094
public async Task GetInformationAsync_ForCorrectFileId_ShouldReturnSameFileAsUploadedFile()
6195
{
@@ -66,6 +100,29 @@ public async Task GetInformationAsync_ForCorrectFileId_ShouldReturnSameFileAsUpl
66100
Assert.AreEqual(uploadedFile.Sha1, downloadedFile.Sha1);
67101
}
68102

103+
[TestMethod]
104+
public async Task GetInformationAsync_ForFileWithSharedLink_ShouldReturnSameFileAsUploadedFile()
105+
{
106+
var folder = await CreateFolderAsAdmin();
107+
var uploadedFile = await CreateSmallFileAsAdmin(folder.Id);
108+
109+
var password = "SuperSecret123";
110+
var sharedLinkRequest = new BoxSharedLinkRequest
111+
{
112+
Access = BoxSharedLinkAccessType.open,
113+
Password = password
114+
};
115+
116+
var sharedLink = await AdminClient.FilesManager.CreateSharedLinkAsync(uploadedFile.Id, sharedLinkRequest);
117+
118+
var sharedItems = await UserClient.SharedItemsManager.SharedItemsAsync(sharedLink.SharedLink.Url, password);
119+
120+
BoxFile file = await UserClient.FilesManager.GetInformationAsync(sharedItems.Id, sharedLink: sharedLink.SharedLink.Url, sharedLinkPassword: password);
121+
122+
Assert.AreEqual(file.Id, uploadedFile.Id);
123+
Assert.AreEqual(file.Sha1, uploadedFile.Sha1);
124+
}
125+
69126
[TestMethod]
70127
public async Task GetDownloadUriAsync_ForCorrectFileId_ShouldReturnCorrectUrl()
71128
{

Box.V2.Test.Integration/BoxFolderManagerIntegrationTest.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,26 @@ public async Task GetInformationAsync_ForExistingFolder_ShouldReturnInformationR
7676
Assert.AreEqual(folder.Id, createdFolder.Id);
7777
}
7878

79+
[TestMethod]
80+
public async Task GetInformationAsync_ForFolderWithSharedLink_ShouldReturnInformationRelatedToThisFolder()
81+
{
82+
var createdFolder = await CreateFolderAsAdmin();
83+
84+
var password = "SuperSecret123";
85+
var sharedLinkRequest = new BoxSharedLinkRequest
86+
{
87+
Access = BoxSharedLinkAccessType.open,
88+
Password = password
89+
};
90+
var sharedLink = await AdminClient.FoldersManager.CreateSharedLinkAsync(createdFolder.Id, sharedLinkRequest);
91+
92+
var sharedItems = await UserClient.SharedItemsManager.SharedItemsAsync(sharedLink.SharedLink.Url, password);
93+
94+
BoxFolder folder = await UserClient.FoldersManager.GetInformationAsync(sharedItems.Id, sharedLink: sharedLink.SharedLink.Url, sharedLinkPassword: password);
95+
96+
Assert.AreEqual(folder.Id, createdFolder.Id);
97+
}
98+
7999
[TestMethod]
80100
public async Task CreateSharedLinkAsync_ForExistingFolder_ShouldCreateSharedLinkToThatFolder()
81101
{

Box.V2/Managers/BoxFilesManager.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,22 @@ public BoxFilesManager(IBoxConfig config, IBoxService service, IBoxConverter con
3636
/// </summary>
3737
/// <param name="id">Id of the file.</param>
3838
/// <param name="fields">Attribute(s) to include in the response.</param>
39+
/// <param name="sharedLink">The shared link for this file</param>
40+
/// <param name="sharedLinkPassword">The password for the shared link (if required)</param>
3941
/// <returns>A full file object is returned if the ID is valid and if the user has access to the file.</returns>
40-
public async Task<BoxFile> GetInformationAsync(string id, IEnumerable<string> fields = null)
42+
public async Task<BoxFile> GetInformationAsync(string id, IEnumerable<string> fields = null, string sharedLink = null, string sharedLinkPassword = null)
4143
{
4244
id.ThrowIfNullOrWhiteSpace("id");
4345

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

49+
if (!string.IsNullOrEmpty(sharedLink))
50+
{
51+
var sharedLinkHeader = SharedLinkUtils.GetSharedLinkHeader(sharedLink, sharedLinkPassword);
52+
request.Header(sharedLinkHeader.Item1, sharedLinkHeader.Item2);
53+
}
54+
4755
IBoxResponse<BoxFile> response = await ToResponseAsync<BoxFile>(request).ConfigureAwait(false);
4856

4957
return response.ResponseObject;
@@ -57,8 +65,10 @@ public async Task<BoxFile> GetInformationAsync(string id, IEnumerable<string> fi
5765
/// <param name="timeout">Optional timeout for response.</param>
5866
/// <param name="startOffsetInBytes">Starting byte of the chunk to download.</param>
5967
/// <param name="endOffsetInBytes">Ending byte of the chunk to download.</param>
68+
/// <param name="sharedLink">The shared link for this file</param>
69+
/// <param name="sharedLinkPassword">The password for the shared link (if required)</param>
6070
/// <returns>Stream of the requested file.</returns>
61-
public async Task<Stream> DownloadAsync(string id, string versionId = null, TimeSpan? timeout = null, long? startOffsetInBytes = null, long? endOffsetInBytes = null)
71+
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)
6272
{
6373
id.ThrowIfNullOrWhiteSpace("id");
6474

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

83+
if (!string.IsNullOrEmpty(sharedLink))
84+
{
85+
var sharedLinkHeader = SharedLinkUtils.GetSharedLinkHeader(sharedLink, sharedLinkPassword);
86+
request.Header(sharedLinkHeader.Item1, sharedLinkHeader.Item2);
87+
}
88+
7389
IBoxResponse<Stream> response = await ToResponseAsync<Stream>(request).ConfigureAwait(false);
7490
return response.ResponseObject;
7591
}

Box.V2/Managers/BoxFoldersManager.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,24 @@ public async Task<BoxFolder> CreateAsync(BoxFolderRequest folderRequest, IEnumer
9696
/// </summary>
9797
/// <param name="id">The folder id</param>
9898
/// <param name="fields">Attribute(s) to include in the response</param>
99-
/// <returns>A full folder object is returned, including the most current information available about it.
99+
/// <param name="sharedLink">The shared link for this folder</param>
100+
/// <param name="sharedLinkPassword">The password for the shared link (if required)</param>
101+
/// <returns>A full folder object is returned, including the most current information available about it.
100102
/// An exception is thrown if the folder does not exist or if the user does not have access to it.</returns>
101-
public async Task<BoxFolder> GetInformationAsync(string id, IEnumerable<string> fields = null)
103+
public async Task<BoxFolder> GetInformationAsync(string id, IEnumerable<string> fields = null, string sharedLink = null, string sharedLinkPassword = null)
102104
{
103105
id.ThrowIfNullOrWhiteSpace("id");
104106

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

111+
if (!string.IsNullOrEmpty(sharedLink))
112+
{
113+
var sharedLinkHeader = SharedLinkUtils.GetSharedLinkHeader(sharedLink, sharedLinkPassword);
114+
request.Header(sharedLinkHeader.Item1, sharedLinkHeader.Item2);
115+
}
116+
109117
IBoxResponse<BoxFolder> response = await ToResponseAsync<BoxFolder>(request).ConfigureAwait(false);
110118

111119
return response.ResponseObject;

Box.V2/Managers/IBoxFilesManager.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ public interface IBoxFilesManager
1919
/// </summary>
2020
/// <param name="id">Id of the file.</param>
2121
/// <param name="fields">Attribute(s) to include in the response.</param>
22+
/// <param name="sharedLink">The shared link for this file</param>
23+
/// <param name="sharedLinkPassword">The password for the shared link (if required)</param>
2224
/// <returns>A full file object is returned if the ID is valid and if the user has access to the file.</returns>
23-
Task<BoxFile> GetInformationAsync(string id, IEnumerable<string> fields = null);
25+
Task<BoxFile> GetInformationAsync(string id, IEnumerable<string> fields = null, string sharedLink = null, string sharedLinkPassword = null);
2426

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

3640
/// <summary>
3741
/// 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.

Box.V2/Managers/IBoxFoldersManager.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,11 @@ Task<BoxCollection<BoxItem>> GetFolderItemsAsync(string id, int limit, int offse
4545
/// </summary>
4646
/// <param name="id">The folder id</param>
4747
/// <param name="fields">Attribute(s) to include in the response</param>
48-
/// <returns>A full folder object is returned, including the most current information available about it.
48+
/// <param name="sharedLink">The shared link for this folder</param>
49+
/// <param name="sharedLinkPassword">The password for the shared link (if required)</param>
50+
/// <returns>A full folder object is returned, including the most current information available about it.
4951
/// An exception is thrown if the folder does not exist or if the user does not have access to it.</returns>
50-
Task<BoxFolder> GetInformationAsync(string id, IEnumerable<string> fields = null);
52+
Task<BoxFolder> GetInformationAsync(string id, IEnumerable<string> fields = null, string sharedLink = null, string sharedLinkPassword = null);
5153

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

0 commit comments

Comments
 (0)