Skip to content

Commit

Permalink
Add support for context and structured metadata in Rename Admin…
Browse files Browse the repository at this point in the history
… API
  • Loading branch information
YomesInc authored Jul 25, 2021
1 parent 49746eb commit dd9ee4a
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 18 deletions.
97 changes: 85 additions & 12 deletions CloudinaryDotNet.IntegrationTests/UploadApi/RenameMethodsTest.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
using System.Net;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using CloudinaryDotNet.Actions;
using NUnit.Framework;

namespace CloudinaryDotNet.IntegrationTests.UploadApi
{
public class RenameMethodsTest : IntegrationTestBase
{
private static string m_context;

public override void Initialize()
{
base.Initialize();

var contextKey = $"{m_uniqueTestId}_context_key";
var contextValue = $"{m_uniqueTestId}_context_value";
m_context = $"{contextKey}={contextValue}";
}

[Test, RetryWithDelay]
public void TestRename()
public async Task TestRename()
{
var toPublicId = GetUniquePublicId();

Expand All @@ -16,29 +29,29 @@ public void TestRename()
File = new FileDescription(m_testImagePath),
Tags = m_apiTag
};
var uploadResult1 = m_cloudinary.Upload(uploadParams);
var uploadResult1 = await m_cloudinary.UploadAsync(uploadParams);

uploadParams.File = new FileDescription(m_testIconPath);
var uploadResult2 = m_cloudinary.Upload(uploadParams);
var uploadResult2 = await m_cloudinary.UploadAsync(uploadParams);

var renameResult = m_cloudinary.Rename(uploadResult1.PublicId, toPublicId);
var renameResult = await m_cloudinary.RenameAsync(uploadResult1.PublicId, toPublicId);
Assert.AreEqual(HttpStatusCode.OK, renameResult.StatusCode, renameResult.Error?.Message);

var getResult = m_cloudinary.GetResource(toPublicId);
var getResult = await m_cloudinary.GetResourceAsync(toPublicId);
Assert.NotNull(getResult);

renameResult = m_cloudinary.Rename(uploadResult2.PublicId, toPublicId);
renameResult = await m_cloudinary.RenameAsync(uploadResult2.PublicId, toPublicId);
Assert.True(renameResult.StatusCode == HttpStatusCode.BadRequest, renameResult.Error?.Message);

m_cloudinary.Rename(uploadResult2.PublicId, toPublicId, true);
await m_cloudinary.RenameAsync(uploadResult2.PublicId, toPublicId, true);

getResult = m_cloudinary.GetResource(toPublicId);
getResult = await m_cloudinary.GetResourceAsync(toPublicId);
Assert.NotNull(getResult);
Assert.AreEqual(FILE_FORMAT_ICO, getResult.Format, getResult.Error?.Message);
}

[Test, RetryWithDelay]
public void TestRenameToType()
public async Task TestRenameToType()
{
string publicId = GetUniquePublicId();
string newPublicId = GetUniquePublicId();
Expand All @@ -51,18 +64,78 @@ public void TestRenameToType()
Type = STORAGE_TYPE_UPLOAD
};

var uploadResult = m_cloudinary.Upload(uploadParams);
var uploadResult = await m_cloudinary.UploadAsync(uploadParams);
Assert.AreEqual(uploadResult.StatusCode, HttpStatusCode.OK, uploadResult.Error?.Message);

RenameParams renameParams = new RenameParams(publicId, newPublicId)
{
ToType = STORAGE_TYPE_UPLOAD
};

var renameResult = m_cloudinary.Rename(renameParams);
var renameResult = await m_cloudinary.RenameAsync(renameParams);
Assert.AreEqual(renameResult.StatusCode, HttpStatusCode.OK, renameResult.Error?.Message);
Assert.AreEqual(renameResult.Type, STORAGE_TYPE_UPLOAD);
Assert.AreEqual(renameResult.PublicId, newPublicId);
}

[Test]
public async Task TestRenameReturnsContext()
{
string publicId = GetUniquePublicId();
string newPublicId = GetUniquePublicId();

await UploadImage(publicId);

var @params = new RenameParams(publicId, newPublicId)
{
Context = true
};
var renameResult = await m_cloudinary.RenameAsync(@params);
Assert.IsTrue(renameResult.Context.HasValues);

@params.Context = false;
renameResult = await m_cloudinary.RenameAsync(@params);
Assert.IsNull(renameResult.Context);
}

[Test]
public async Task TestRenameReturnsMetadata()
{
string publicId = GetUniquePublicId();
string newPublicId = GetUniquePublicId();

await UploadImage(publicId, true);

var @params = new RenameParams(publicId, newPublicId)
{
Metadata = true
};
var renameResult = await m_cloudinary.RenameAsync(@params);
Assert.IsTrue(renameResult.MetadataFields.HasValues);

@params.Metadata = false;
renameResult = await m_cloudinary.RenameAsync(@params);
Assert.IsNull(renameResult.MetadataFields);
}

private async Task UploadImage(string publicId, bool withMetadata = false)
{
if (withMetadata)
{
CreateMetadataField("rename_with_metadata", p => p.DefaultValue = p.Label);
}

var uploadParams = new ImageUploadParams()
{
PublicId = publicId,
File = new FileDescription(m_testImagePath),
Tags = m_apiTag,
Type = STORAGE_TYPE_UPLOAD,
Context = new StringDictionary(m_context),
};

var uploadResult = await m_cloudinary.UploadAsync(uploadParams);
Assert.AreEqual(uploadResult.StatusCode, HttpStatusCode.OK, uploadResult.Error?.Message);
}
}
}
21 changes: 21 additions & 0 deletions CloudinaryDotNet/Actions/AssetsUpload/RenameParams.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ public RenameParams(string fromPublicId, string toPublicId)
/// </value>
public bool Invalidate { get; set; }

/// <summary>
/// Gets or sets a value indicating whether if true, include context assigned to the resource.
/// </summary>
public bool Context { get; set; }

/// <summary>
/// Gets or sets a value indicating whether if true, include metadata assigned to the resource.
/// </summary>
public bool Metadata { get; set; }

/// <summary>
/// Maps object model to dictionary of parameters in cloudinary notation.
/// </summary>
Expand All @@ -84,6 +94,17 @@ public override SortedDictionary<string, object> ToParamsDictionary()
AddParam(dict, "type", Type);
AddParam(dict, "to_type", ToType);
AddParam(dict, "invalidate", Invalidate);

if (Context)
{
AddParam(dict, "context", Context);
}

if (Metadata)
{
AddParam(dict, "metadata", Metadata);
}

return dict;
}

Expand Down
17 changes: 11 additions & 6 deletions CloudinaryDotNet/Cloudinary.UploadApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,11 @@ public T UploadLarge<T>(BasicRawUploadParams parameters, int bufferSize = DEFAUL
/// <param name="overwrite">Overwrite a file with the same identifier as new if such file exists.</param>
/// <param name="cancellationToken">(Optional) Cancellation token.</param>
/// <returns>Result of resource renaming.</returns>
public Task<RenameResult> RenameAsync(string fromPublicId, string toPublicId, bool overwrite = false, CancellationToken? cancellationToken = null)
public Task<RenameResult> RenameAsync(
string fromPublicId,
string toPublicId,
bool overwrite = false,
CancellationToken? cancellationToken = null)
{
return RenameAsync(
new RenameParams(fromPublicId, toPublicId)
Expand All @@ -355,11 +359,12 @@ public Task<RenameResult> RenameAsync(string fromPublicId, string toPublicId, bo
/// <returns>Result of resource renaming.</returns>
public RenameResult Rename(string fromPublicId, string toPublicId, bool overwrite = false)
{
return Rename(
new RenameParams(fromPublicId, toPublicId)
{
Overwrite = overwrite,
});
var renameParams = new RenameParams(fromPublicId, toPublicId)
{
Overwrite = overwrite,
};

return RenameAsync(renameParams).GetAwaiter().GetResult();
}

/// <summary>
Expand Down

0 comments on commit dd9ee4a

Please sign in to comment.