Skip to content

Commit

Permalink
Specify original filename, support custom parameters, include more as…
Browse files Browse the repository at this point in the history
…set properties during upload, upload file as stream
  • Loading branch information
quirijnslings committed Sep 4, 2024
1 parent ec6a52b commit ac3c7e3
Show file tree
Hide file tree
Showing 6 changed files with 227 additions and 19 deletions.
74 changes: 72 additions & 2 deletions Bynder/Sample/UploadSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using System.Threading.Tasks;
using System.Linq;
using Bynder.Sdk.Query.Upload;
using System.Collections.Generic;
using System.IO;
namespace Bynder.Sample
{
public class UploadSample
Expand Down Expand Up @@ -41,9 +43,77 @@ private async Task RunUploadSampleAsync()
return;
}

await assetService.UploadFileAsync(new UploadQuery { Filepath = uploadPath, BrandId = brands.First().Id });
Console.WriteLine("Name of the media item after upload: ");
var name = Console.ReadLine();
if (string.IsNullOrEmpty(name))
{
name = null;
}

Console.WriteLine("Override original filename (leave empty to use the actual filename): ");
var filename = Console.ReadLine();

Console.WriteLine("Description (leave empty to use default): ");
var description = Console.ReadLine();

var customParameters = GetCustomParameters();

Console.WriteLine("Do you want to pass a file stream to the SDK?: (y/n)");
var passAsStream = Console.ReadLine().ToLower().StartsWith("y");


var query = new UploadQuery
{
Filepath = uploadPath,
BrandId = brands.First().Id,
Name = name,
CustomParameters = customParameters
};
if (!string.IsNullOrEmpty(filename))
{
query.OriginalFileName = filename;
}
if (!string.IsNullOrEmpty(description))
{
query.Description = description;
}

FileStream fileStream = null;
if (passAsStream)
{
fileStream = File.Open(query.Filepath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete);
}
var before = DateTime.Now;
var response = passAsStream ? await assetService.UploadFileAsync(fileStream, query) : await assetService.UploadFileAsync(query);
var ms = Math.Round((DateTime.Now - before).TotalMilliseconds);
Console.WriteLine($"Uploaded file as media with id {response.MediaId} (time elapsed: {ms})");

}


private Dictionary<string,string> GetCustomParameters()
{
Console.WriteLine("Do you want to add custom parameters during the upload? (y/n)");
var input = Console.ReadLine();

if (!input.ToString().ToLower().StartsWith("y"))
{
return null;
}

Dictionary<string,string> parameters = new Dictionary<string,string>();
while (input.ToString().ToLower().StartsWith("y"))
{
Console.WriteLine("Parameter name: ");
var paramName = Console.ReadLine();
Console.WriteLine("Parameter value: ");
var paramValue = Console.ReadLine();
parameters.Add(paramName, paramValue);
Console.WriteLine("Do you want to add another custom parameter? (y/n)");
input = Console.ReadLine();
}
return parameters;
}

private async Task AuthenticateWithOAuth2Async(bool useClientCredentials)
{
if (useClientCredentials)
Expand Down
27 changes: 26 additions & 1 deletion Bynder/Sdk/Query/Upload/SaveMediaQuery.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Bynder. All rights reserved.
// Copyright (c) Bynder. All rights reserved.
// Licensed under the MIT License. See LICENSE file in the project root for full license information.

using System.Collections.Generic;
Expand Down Expand Up @@ -43,6 +43,31 @@ internal class SaveMediaQuery
[ApiField("tags", Converter = typeof(ListConverter))]
public IList<string> Tags { get; set; }


/// <summary>
/// Description of the media
/// </summary>
[ApiField("description")]
public string Description { get; set; }

/// <summary>
/// Published date of the media
/// </summary>
[ApiField("ISOPublicationDate")]
public string PublishedDate { get; set; }

/// <summary>
/// Copyright information for the media
/// </summary>
[ApiField("copyright")]
public string Copyright { get; set; }

/// <summary>
/// Indicates if the media is public
/// </summary>
[ApiField("isPublic")]
public bool IsPublic { get; set; }

/// <summary>
/// Metaproperty options to set on the asset.
/// </summary>
Expand Down
44 changes: 43 additions & 1 deletion Bynder/Sdk/Query/Upload/UploadQuery.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Bynder. All rights reserved.
// Copyright (c) Bynder. All rights reserved.
// Licensed under the MIT License. See LICENSE file in the project root for full license information.

using System.Collections.Generic;
Expand All @@ -15,6 +15,16 @@ public class UploadQuery
/// </summary>
public string Filepath { get; set; }

/// <summary>
/// Name the media will have.
/// </summary>
public string Name { get; set; }

/// <summary>
/// Original file name the media will have.
/// </summary>
public string OriginalFileName { get; set; }

/// <summary>
/// Brand id where we want to store the file
/// </summary>
Expand All @@ -31,5 +41,37 @@ public class UploadQuery
/// Tags of the file that we want to update
/// </summary>
public IList<string> Tags { get; set; }

/// <summary>
/// Description of the media
/// </summary>
public string Description { get; set; }

/// <summary>
/// Copyright information for the media
/// </summary>
public string Copyright { get; set; }

/// <summary>
/// Indicates if the media is public
/// </summary>
public bool IsPublic { get; set; }

/// <summary>
/// Metaproperties the media will have
/// </summary>
public IDictionary<string, IList<string>> MetapropertyOptions { get; set; } = new Dictionary<string, IList<string>>();

/// <summary>
/// Published date the media will have.
/// </summary>
public string PublishedDate { get; set; }

/// <summary>
/// Custom parameters to add to the upload endpoint
/// </summary>
public IEnumerable<KeyValuePair<string,string>> CustomParameters { get; set; }


}
}
14 changes: 14 additions & 0 deletions Bynder/Sdk/Service/Asset/AssetService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Bynder.Sdk.Model;
using Bynder.Sdk.Query.Asset;
using Bynder.Sdk.Query.Upload;
using System.IO;

namespace Bynder.Sdk.Service.Asset
{
Expand Down Expand Up @@ -143,6 +144,18 @@ public async Task<SaveMediaResponse> UploadFileAsync(UploadQuery query)
return await _uploader.UploadFileAsync(query).ConfigureAwait(false);
}

/// <summary>
/// Check <see cref="IAssetService"/> for more information
/// </summary>
/// <param name="fileStream">Check <see cref="IAssetService"/> for more information</param>
/// <param name="query">Check <see cref="IAssetService"/> for more information</param>
/// <returns>Check <see cref="IAssetService"/> for more information</returns>
public async Task<SaveMediaResponse> UploadFileAsync(FileStream fileStream, UploadQuery query)
{
return await _uploader.UploadFileAsync(fileStream, query).ConfigureAwait(false);
}


/// <summary>
/// Check <see cref="IAssetService"/> for more information
/// </summary>
Expand Down Expand Up @@ -266,5 +279,6 @@ private static MediaQueryFull CloneIntoFullMediaQuery(MediaQuery query)
Total = true
};
}

}
}
15 changes: 14 additions & 1 deletion Bynder/Sdk/Service/Asset/IAssetService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Bynder.Sdk.Model;
using Bynder.Sdk.Query.Asset;
Expand Down Expand Up @@ -74,14 +75,26 @@ public interface IAssetService
Task<IList<Media>> GetMediaListAsync(MediaQuery query);

/// <summary>
/// Uploads a file async.
/// Uploads a file based on a filepath in the query
/// </summary>
/// <param name="query">Information to upload a file</param>
/// <returns>Task representing the upload</returns>
/// <exception cref="HttpRequestException">Can be thrown when requests to server can't be completed or HTTP code returned by server is an error</exception>
/// <exception cref="BynderUploadException">Can be thrown when upload does not finish within expected time</exception>
Task<SaveMediaResponse> UploadFileAsync(UploadQuery query);

/// <summary>
/// Uploads a file as a stream
/// </summary>
/// <param name="fileStream">Stream representing the file to be uploaded</param>
/// <param name="query">Information to upload a file</param>
/// <returns>Task representing the upload</returns>
/// <exception cref="HttpRequestException">Can be thrown when requests to server can't be completed or HTTP code returned by server is an error</exception>
/// <exception cref="BynderUploadException">Can be thrown when upload does not finish within expected time</exception>

Task<SaveMediaResponse> UploadFileAsync(FileStream fileStream, UploadQuery query);


/// <summary>
/// Modifies a media
/// </summary>
Expand Down
Loading

0 comments on commit ac3c7e3

Please sign in to comment.