Advertisement
joelmello

C# Cloudinary Api Client Wrapper

Dec 2nd, 2016
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.63 KB | None | 0 0
  1. using CloudinaryDotNet;
  2. using CloudinaryDotNet.Actions;
  3. using ExampleApp.Framework.Diagnostics;
  4. using System;
  5.  
  6. namespace ExampleApp.Integration.ImageServices.CloudinaryClient
  7. {
  8.     public class CloudinaryImageService
  9.     {
  10.         // Private properties, constants        
  11.         protected string ApiSecret { get; set; }
  12.         protected string ApiKey { get; set; }
  13.         protected string CloudName { get; set; }
  14.  
  15.         protected Cloudinary ApiClient { get; set; }
  16.  
  17.         // ctors
  18.         public static CloudinaryImageService Create(string cloudName, string apiKey, string apiSecret)
  19.         {
  20.             var service = new CloudinaryImageService() { ApiKey = apiKey, CloudName = cloudName, ApiSecret = apiSecret };
  21.  
  22.             service.ApiClient = new CloudinaryDotNet.Cloudinary(new Account(cloudName, apiKey, apiSecret));
  23.  
  24.             return service;
  25.         }
  26.  
  27.         private CloudinaryImageService()
  28.         {
  29.  
  30.         }
  31.  
  32.         // Methods
  33.         public InfoResult<string> UploadImageFromRemoteUrl(string url, string publicId)
  34.         {
  35.             // PublicId: {{sport}}-headshot-{{athleteid}}
  36.             var result = new InfoResult<string>();
  37.  
  38.             // Validation
  39.             if (string.IsNullOrWhiteSpace(url))
  40.             {
  41.                 result.Add(new ArgumentNullException("url"));
  42.                 return result;
  43.             }
  44.  
  45.             if (string.IsNullOrWhiteSpace(publicId))
  46.             {
  47.                 result.Add(new ArgumentNullException("publicId"));
  48.                 return result;
  49.             }
  50.  
  51.  
  52.             // Configure upload parameters
  53.             var uploadParams = new ImageUploadParams();
  54.             uploadParams.File = new FileDescription(url);
  55.             uploadParams.PublicId = publicId;
  56.  
  57.             try
  58.             {
  59.                 // Attempt upload
  60.                 var uploadResult = ApiClient.Upload(uploadParams);
  61.  
  62.                 if (uploadResult.Error != null && !string.IsNullOrWhiteSpace(uploadResult.Error.Message))
  63.                 {
  64.                     result.Add(string.Format("Error uploading image: {0}", uploadResult.Error.Message));
  65.                     return result;
  66.                 }
  67.  
  68.                 result.Value = uploadResult.SecureUri.ToString();
  69.             }
  70.             catch (Exception ex)
  71.             {
  72.                 result.Add(ex);
  73.             }
  74.  
  75.             return result;
  76.         }
  77.  
  78.         public InfoList DeleteImagesByPrefix(string prefix)
  79.         {
  80.             var result = new InfoList();
  81.  
  82.             var deleteResult = ApiClient.DeleteResourcesByPrefix(prefix);
  83.  
  84.             return result;
  85.         }
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement