Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using CloudinaryDotNet;
- using CloudinaryDotNet.Actions;
- using ExampleApp.Framework.Diagnostics;
- using System;
- namespace ExampleApp.Integration.ImageServices.CloudinaryClient
- {
- public class CloudinaryImageService
- {
- // Private properties, constants
- protected string ApiSecret { get; set; }
- protected string ApiKey { get; set; }
- protected string CloudName { get; set; }
- protected Cloudinary ApiClient { get; set; }
- // ctors
- public static CloudinaryImageService Create(string cloudName, string apiKey, string apiSecret)
- {
- var service = new CloudinaryImageService() { ApiKey = apiKey, CloudName = cloudName, ApiSecret = apiSecret };
- service.ApiClient = new CloudinaryDotNet.Cloudinary(new Account(cloudName, apiKey, apiSecret));
- return service;
- }
- private CloudinaryImageService()
- {
- }
- // Methods
- public InfoResult<string> UploadImageFromRemoteUrl(string url, string publicId)
- {
- // PublicId: {{sport}}-headshot-{{athleteid}}
- var result = new InfoResult<string>();
- // Validation
- if (string.IsNullOrWhiteSpace(url))
- {
- result.Add(new ArgumentNullException("url"));
- return result;
- }
- if (string.IsNullOrWhiteSpace(publicId))
- {
- result.Add(new ArgumentNullException("publicId"));
- return result;
- }
- // Configure upload parameters
- var uploadParams = new ImageUploadParams();
- uploadParams.File = new FileDescription(url);
- uploadParams.PublicId = publicId;
- try
- {
- // Attempt upload
- var uploadResult = ApiClient.Upload(uploadParams);
- if (uploadResult.Error != null && !string.IsNullOrWhiteSpace(uploadResult.Error.Message))
- {
- result.Add(string.Format("Error uploading image: {0}", uploadResult.Error.Message));
- return result;
- }
- result.Value = uploadResult.SecureUri.ToString();
- }
- catch (Exception ex)
- {
- result.Add(ex);
- }
- return result;
- }
- public InfoList DeleteImagesByPrefix(string prefix)
- {
- var result = new InfoList();
- var deleteResult = ApiClient.DeleteResourcesByPrefix(prefix);
- return result;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement