Skip to content

Commit

Permalink
Added file prefix and delimiters, only minor testing done.
Browse files Browse the repository at this point in the history
  • Loading branch information
coryrwest committed Dec 29, 2017
1 parent 43f087a commit e93dfb0
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 73 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ var bucket = client.Buckets.Delete("BUCKETID").Result;
var client = new B2Client(options);
options = client.Authorize().Result;
var fileList = client.Files.GetList("BUCKETID", "FILENAME").Result;
// Using optional prefix and delimiter
var fileList = client.Files.GetList("BUCKETID", "FILENAME", prefix: "PREFIX", delimiter: "DELIMITER").Result;
// {
// NextFileName: "",
// [
Expand Down
93 changes: 63 additions & 30 deletions src/Files.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,49 +20,82 @@ public Files(B2Options options) {
}

/// <summary>
/// Lists the names of all non-hidden files in a bucket, starting at a given name.
/// Lists the names of all non-hidden files in a bucket, starting at a given name.
/// </summary>
/// <param name="bucketId"></param>
/// <param name="startFileName"></param>
/// <param name="maxFileCount"></param>
/// <param name="cancelToken"></param>
/// <returns></returns>
public async Task<B2FileList> GetList(string startFileName = "", int? maxFileCount = null, string bucketId = "", CancellationToken cancelToken = default(CancellationToken)) {
var operationalBucketId = Utilities.DetermineBucketId(_options, bucketId);
return await GetListWithPrefixOrDemiliter(startFileName, "", "", maxFileCount, bucketId, cancelToken);
}

var requestMessage = FileMetaDataRequestGenerators.GetList(_options, operationalBucketId, startFileName, maxFileCount);
var response = await _client.SendAsync(requestMessage, cancelToken);
/// <summary>
/// BETA: Lists the names of all non-hidden files in a bucket, starting at a given name. With an optional file prefix or delimiter.
/// See here for more details: https://www.backblaze.com/b2/docs/b2_list_file_names.html
/// </summary>
/// <param name="startFileName"></param>
/// <param name="prefix"></param>
/// <param name="delimiter"></param>
/// <param name="maxFileCount"></param>
/// <param name="bucketId"></param>
/// <param name="cancelToken"></param>
/// <returns></returns>
public async Task<B2FileList> GetListWithPrefixOrDemiliter(string startFileName = "", string prefix = "", string delimiter = "", int? maxFileCount = null, string bucketId = "", CancellationToken cancelToken = default(CancellationToken)) {
var operationalBucketId = Utilities.DetermineBucketId(_options, bucketId);

return await ResponseParser.ParseResponse<B2FileList>(response);
}
var requestMessage = FileMetaDataRequestGenerators.GetList(_options, operationalBucketId, startFileName, maxFileCount, prefix, delimiter);
var response = await _client.SendAsync(requestMessage, cancelToken);

/// <summary>
/// Lists all of the versions of all of the files contained in one bucket,
/// in alphabetical order by file name, and by reverse of date/time uploaded
/// for versions of files with the same name.
/// </summary>
/// <param name="startFileName"></param>
/// <param name="startFileId"></param>
/// <param name="maxFileCount"></param>
/// <param name="bucketId"></param>
/// <param name="cancelToken"></param>
/// <returns></returns>
public async Task<B2FileList> GetVersions(string startFileName = "", string startFileId = "", int? maxFileCount = null, string bucketId = "", CancellationToken cancelToken = default(CancellationToken)) {
var operationalBucketId = Utilities.DetermineBucketId(_options, bucketId);
return await ResponseParser.ParseResponse<B2FileList>(response);
}

var requestMessage = FileMetaDataRequestGenerators.ListVersions(_options, operationalBucketId, startFileName, startFileId, maxFileCount);
var response = await _client.SendAsync(requestMessage, cancelToken);
/// <summary>
/// Lists all of the versions of all of the files contained in one bucket,
/// in alphabetical order by file name, and by reverse of date/time uploaded
/// for versions of files with the same name.
/// </summary>
/// <param name="startFileName"></param>
/// <param name="startFileId"></param>
/// <param name="maxFileCount"></param>
/// <param name="bucketId"></param>
/// <param name="cancelToken"></param>
/// <returns></returns>
public async Task<B2FileList> GetVersions(string startFileName = "", string startFileId = "", int? maxFileCount = null, string bucketId = "", CancellationToken cancelToken = default(CancellationToken)) {
return await GetVersionsWithPrefixOrDelimiter(startFileName, startFileId, "", "", maxFileCount, bucketId, cancelToken);
}

return await ResponseParser.ParseResponse<B2FileList>(response);
}
/// <summary>
/// BETA: Lists all of the versions of all of the files contained in one bucket,
/// in alphabetical order by file name, and by reverse of date/time uploaded
/// for versions of files with the same name. With an optional file prefix or delimiter.
/// See here for more details: https://www.backblaze.com/b2/docs/b2_list_file_versions.html
/// </summary>
/// <param name="startFileName"></param>
/// <param name="startFileId"></param>
/// <param name="prefix"></param>
/// <param name="delimiter"></param>
/// <param name="maxFileCount"></param>
/// <param name="bucketId"></param>
/// <param name="cancelToken"></param>
/// <returns></returns>
public async Task<B2FileList> GetVersionsWithPrefixOrDelimiter(string startFileName = "", string startFileId = "", string prefix = "", string delimiter = "", int? maxFileCount = null, string bucketId = "", CancellationToken cancelToken = default(CancellationToken)) {
var operationalBucketId = Utilities.DetermineBucketId(_options, bucketId);

/// <summary>
/// Gets information about one file stored in B2.
/// </summary>
/// <param name="fileId"></param>
/// <param name="cancelToken"></param>
/// <returns></returns>
public async Task<B2File> GetInfo(string fileId, CancellationToken cancelToken = default(CancellationToken)) {
var requestMessage = FileMetaDataRequestGenerators.ListVersions(_options, operationalBucketId, startFileName, startFileId, maxFileCount, prefix, delimiter);
var response = await _client.SendAsync(requestMessage, cancelToken);

return await ResponseParser.ParseResponse<B2FileList>(response);
}

/// <summary>
/// Gets information about one file stored in B2.
/// </summary>
/// <param name="fileId"></param>
/// <param name="cancelToken"></param>
/// <returns></returns>
public async Task<B2File> GetInfo(string fileId, CancellationToken cancelToken = default(CancellationToken)) {
var requestMessage = FileMetaDataRequestGenerators.GetInfo(_options, fileId);
var response = await _client.SendAsync(requestMessage, cancelToken);

Expand Down
26 changes: 19 additions & 7 deletions src/Http/RequestGenerators/FileMetaDataRequestGenerators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,25 @@ private static class Endpoints {
public const string Info = "b2_get_file_info";
}

public static HttpRequestMessage GetList(B2Options options, string bucketId, string startFileName = "", int? maxFileCount = null) {
public static HttpRequestMessage GetList(B2Options options, string bucketId, string startFileName = "", int? maxFileCount = null, string prefix = "", string delimiter = "") {
var body = "{\"bucketId\":\"" + bucketId + "\"";
if (!string.IsNullOrEmpty(startFileName)) {
body += ", \"startFileName\":" + JsonConvert.ToString(startFileName);
}
if (maxFileCount.HasValue) {
body += ", \"maxFileCount\":" + maxFileCount.Value.ToString();
}
if (maxFileCount.HasValue) {
body += ", \"maxFileCount\":" + maxFileCount.Value.ToString();
}
body += "}";
if (!string.IsNullOrEmpty(prefix)) {
body += ", \"prefix\":" + JsonConvert.ToString(prefix);
}
if (!string.IsNullOrEmpty(delimiter)) {
body += ", \"delimiter\":" + JsonConvert.ToString(delimiter);
}
body += "}";
return BaseRequestGenerator.PostRequest(Endpoints.List, body, options);
}

public static HttpRequestMessage ListVersions(B2Options options, string bucketId, string startFileName = "", string startFileId = "", int? maxFileCount = null) {
public static HttpRequestMessage ListVersions(B2Options options, string bucketId, string startFileName = "", string startFileId = "", int? maxFileCount = null, string prefix = "", string delimiter = "") {
var body = "{\"bucketId\":\"" + bucketId + "\"";
if (!string.IsNullOrEmpty(startFileName)) {
body += ", \"startFileName\":" + JsonConvert.ToString(startFileName);
Expand All @@ -35,7 +41,13 @@ public static HttpRequestMessage ListVersions(B2Options options, string bucketId
if (maxFileCount.HasValue) {
body += ", \"maxFileCount\":" + maxFileCount.Value.ToString();
}
body += "}";
if (!string.IsNullOrEmpty(prefix)) {
body += ", \"prefix\":" + JsonConvert.ToString(prefix);
}
if (!string.IsNullOrEmpty(delimiter)) {
body += ", \"delimiter\":" + JsonConvert.ToString(delimiter);
}
body += "}";
return BaseRequestGenerator.PostRequest(Endpoints.Versions, body, options);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.0.4.0")]
[assembly: AssemblyFileVersion("0.0.4.0")]
[assembly: AssemblyVersion("0.0.4.5")]
[assembly: AssemblyFileVersion("0.0.4.5")]
120 changes: 86 additions & 34 deletions tests/FileTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using B2Net.Http;
using B2Net.Models;
using Microsoft.VisualStudio.TestTools.UnitTesting;
Expand Down Expand Up @@ -36,50 +37,101 @@ public void Initialize() {
} else {
TestBucket = Client.Buckets.Create("B2NETTestingBucket", BucketTypes.allPrivate).Result;
}
}

[TestMethod]
public void GetListTest() {
var fileName = "B2Test.txt";
var fileData = File.ReadAllBytes(Path.Combine(FilePath, fileName));
var file = Client.Files.Upload(fileData, fileName, TestBucket.BucketId).Result;
// Clean up.
FilesToDelete.Add(file);
}

[TestMethod]
public void GetListTest() {
var fileName = "B2Test.txt";
var fileData = File.ReadAllBytes(Path.Combine(FilePath, fileName));
var file = Client.Files.Upload(fileData, fileName, TestBucket.BucketId).Result;
// Clean up.
FilesToDelete.Add(file);

var list = Client.Files.GetList(bucketId: TestBucket.BucketId).Result.Files;

Assert.AreEqual(1, list.Count, list.Count + " files found.");
}

[TestMethod]
public void GetListWithPrefixTest() {
var fileName = "B2Test.txt";
var fileNameWithFolder = "test/B2Test.txt";
var fileData = File.ReadAllBytes(Path.Combine(FilePath, fileName));
var file = Client.Files.Upload(fileData, fileName, TestBucket.BucketId).Result;
var fileFolder = Client.Files.Upload(fileData, fileNameWithFolder, TestBucket.BucketId).Result;
// Clean up.
FilesToDelete.Add(file);
FilesToDelete.Add(fileFolder);

var list = Client.Files.GetListWithPrefixOrDemiliter(bucketId: TestBucket.BucketId, prefix: "test").Result.Files;

Assert.AreEqual(1, list.Count, list.Count + " files found.");
}

[TestMethod]
public void GetListWithPrefixAndDelimiterTest() {
var fileName = "B2Test.txt";
var fileNameWithFolder = "test/B2Test.txt";
var fileData = File.ReadAllBytes(Path.Combine(FilePath, fileName));
var file = Client.Files.Upload(fileData, fileName, TestBucket.BucketId).Result;
var fileFolder = Client.Files.Upload(fileData, fileNameWithFolder, TestBucket.BucketId).Result;
// Clean up.
FilesToDelete.Add(file);
FilesToDelete.Add(fileFolder);

var list = Client.Files.GetListWithPrefixOrDemiliter(bucketId: TestBucket.BucketId, prefix: "test", delimiter: "/").Result.Files;

Assert.AreEqual(1, list.Count, list.Count + " files found.");
Assert.AreEqual("test/", list.First().FileName, "File names to not match.");
}

[TestMethod]
public void GetListWithDelimiterTest() {
var fileName = "B2Test.txt";
var fileNameWithFolder = "test/B2Test.txt";
var fileNameWithFolder2 = "test2/B2Test.txt";
var fileData = File.ReadAllBytes(Path.Combine(FilePath, fileName));
var file = Client.Files.Upload(fileData, fileNameWithFolder2, TestBucket.BucketId).Result;
var fileFolder = Client.Files.Upload(fileData, fileNameWithFolder, TestBucket.BucketId).Result;
// Clean up.
FilesToDelete.Add(file);
FilesToDelete.Add(fileFolder);

var list = Client.Files.GetList(bucketId: TestBucket.BucketId).Result.Files;
var list = Client.Files.GetListWithPrefixOrDemiliter(bucketId: TestBucket.BucketId, delimiter: "/").Result.Files;

Assert.AreEqual(1, list.Count, list.Count + " files found.");
}
Assert.AreEqual(2, list.Count, list.Count + " files found.");
Assert.IsTrue(list.All(f => f.Action == "folder"), "Not all list items were folders.");
}

//[TestMethod]
//public void EmptyBucket() {
// var list = Client.Files.GetList(bucketId: TestBucket.BucketId).Result.Files;
//[TestMethod]
//public void EmptyBucket() {
// var list = Client.Files.GetList(bucketId: TestBucket.BucketId).Result.Files;

// foreach (B2File b2File in list) {
// var deletedFile = Client.Files.Delete(b2File.FileId, b2File.FileName).Result;
// }
//}
// foreach (B2File b2File in list) {
// var deletedFile = Client.Files.Delete(b2File.FileId, b2File.FileName).Result;
// }
//}

//[TestMethod]
//public void HideFileTest() {
// var fileName = "B2Test.txt";
// var fileData = File.ReadAllBytes(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName));
// string hash = Utilities.GetSHA1Hash(fileData);
// var file = Client.Files.Upload(fileData, fileName, TestBucket.BucketId).Result;
// // Clean up.
// FilesToDelete.Add(file);
//[TestMethod]
//public void HideFileTest() {
// var fileName = "B2Test.txt";
// var fileData = File.ReadAllBytes(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName));
// string hash = Utilities.GetSHA1Hash(fileData);
// var file = Client.Files.Upload(fileData, fileName, TestBucket.BucketId).Result;
// // Clean up.
// FilesToDelete.Add(file);

// Assert.AreEqual(hash, file.ContentSHA1, "File hashes did not match.");
// Assert.AreEqual(hash, file.ContentSHA1, "File hashes did not match.");

// var hiddenFile = Client.Files.Hide(file.FileName, TestBucket.BucketId).Result;
// var hiddenFile = Client.Files.Hide(file.FileName, TestBucket.BucketId).Result;

// Assert.IsTrue(hiddenFile.Action == "hide");
// Assert.IsTrue(hiddenFile.Action == "hide");

// // Unhide the file so we can delete it later
// hiddenFile = Client.Files.Hide(file.FileName, TestBucket.BucketId).Result;
// }
// // Unhide the file so we can delete it later
// hiddenFile = Client.Files.Hide(file.FileName, TestBucket.BucketId).Result;
// }

[TestMethod]
[TestMethod]
public void FileUploadTest() {
var fileName = "B2Test.txt";
var fileData = File.ReadAllBytes(Path.Combine(FilePath, fileName));
Expand Down

0 comments on commit e93dfb0

Please sign in to comment.