forked from nitanandsinghupw/imagekit-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
410 lines (349 loc) · 13.9 KB
/
Copy pathProgram.cs
File metadata and controls
410 lines (349 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
// <copyright file="Program.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
namespace ImagekitSample
{
using System;
using System.Collections.Generic;
using Imagekit;
using Imagekit.Models;
using Imagekit.Models.Response;
using Imagekit.Sdk;
using Newtonsoft.Json.Linq;
using static Imagekit.Models.CustomMetaDataFieldSchemaObject;
internal class Program
{
static void Main(string[] args)
{
// Create Instance of ImageKit
ImagekitClient imagekit = new ImagekitClient("your_public_key", "your_private_key", "https://ik.imagekit.io/your_imagekit_id/endpoint");
#region URL Generation
// Generating URLs
string path = "/default-image.jpg";
Transformation trans = new Transformation()
.Width(400)
.Height(300)
.AspectRatio("4-3")
.Quality(40)
.Crop("force").CropMode("extract").
Focus("left").
Format("jpeg").
Background("A94D34").
Border("5-A94D34").
Rotation(90).
Blur(10).
Named("some_name").
OverlayX(35).
OverlayY(35).
OverlayFocus("bottom").
OverlayHeight(20).
OverlayHeight(20).
OverlayImage("/folder/file.jpg"). // leading slash case
OverlayImageTrim(false).
OverlayImageAspectRatio("4:3").
OverlayImageBackground("0F0F0F").
OverlayImageBorder("10_0F0F0F").
OverlayImageDpr(2).
OverlayImageQuality(50).
OverlayImageCropping("force").
OverlayText("two words").
OverlayTextFontSize(20).
OverlayTextFontFamily("Open Sans").
OverlayTextColor("00FFFF").
OverlayTextTransparency(5).
OverlayTextTypography("b").
OverlayBackground("00AAFF55").
OverlayTextEncoded("b3ZlcmxheSBtYWRlIGVhc3k%3D").
OverlayTextWidth(50).
OverlayTextBackground("00AAFF55").
OverlayTextPadding(40).
OverlayTextInnerAlignment("left").
OverlayRadius(10).
Progressive(true).
Lossless(true).
Trim(5).
Metadata(true).
ColorProfile(true).
DefaultImage("folder/file.jpg/"). //trailing slash case
Dpr(3).
EffectSharpen(10).
EffectUsm("2-2-0.8-0.024").
EffectContrast(true).
EffectGray().
Original().
Raw("h-200).w-300).l-image).i-logo.png).l-end")
;
string imageURL = imagekit.Url(trans).Path(path).TransformationPosition("query").Generate();
Console.WriteLine("Url for first image transformed with height: 300, width: 400 - {0}", imageURL);
///// Generating Signed URL
var imgURL1 = "https://ik.imagekit.io/demo/default-image.jpg";
string[] queryParams = { "b=123", "a=test" };
try
{
var signedUrl = imagekit.Url(new Transformation().Width(400).Height(300))
.Src(imgURL1)
.QueryParameters(queryParams)
.ExpireSeconds(600)
.Signed()
.Generate();
Console.WriteLine("Signed Url for first image transformed with height: 300, width: 400: - {0}", signedUrl);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
#endregion
#region Upload BY URI| Bytes | Base64
// Upload By URI
FileCreateRequest request = new FileCreateRequest
{
Url = new Uri(@"http://www.google.com/images/logos/ps_logo2.png"),
FileName = "test.jpg",
};
Result resp1 = imagekit.Upload(request);
// Upload by bytes
byte[] bytes = System.IO.File.ReadAllBytes(@"image file path");
FileCreateRequest ob = new FileCreateRequest
{
Bytes = bytes,
FileName = Guid.NewGuid().ToString(),
};
List<string> tags = new List<string>
{
"Software",
"Developer",
"Engineer",
};
ob.Tags = tags;
ob.Folder = "demo1";
string customCoordinates = "10,10,20,20";
ob.CustomCoordinates = customCoordinates;
List<string> responseFields = new List<string>
{
"thumbnail",
"tags",
"customCoordinates",
};
ob.ResponseFields = responseFields;
JObject optionsInnerObject = new JObject
{
{ "add_shadow", true },
};
JObject innerObject1 = new JObject
{
{ "name", "remove-bg" },
{ "options", optionsInnerObject },
};
JObject innerObject2 = new JObject
{
{ "name", "google-auto-tagging" },
{ "minConfidence", 10 },
{ "maxTags", 5 },
};
JArray jsonArray = new JArray
{
innerObject1,
innerObject2,
};
ob.Extensions = jsonArray;
ob.WebhookUrl = "https://webhook.site/c78d617f-33bc-40d9-9e61-608999721e2e";
ob.UseUniqueFileName = false;
ob.IsPrivateFileValue = false;
ob.OverwriteFile = false;
ob.OverwriteAiTags = false;
ob.OverwriteTags = false;
ob.OverwriteCustomMetadata = true;
JObject jsonObjectCustomMetadata = new JObject
{
{ "test1", 10 },
};
ob.CustomMetadata = jsonObjectCustomMetadata;
Result resp2 = imagekit.Upload(ob);
// Get Base64
byte[] imageArray = System.IO.File.ReadAllBytes(@"image file path");
string base64ImageRepresentation = Convert.ToBase64String(imageArray);
// Upload by Base64
FileCreateRequest ob2 = new FileCreateRequest
{
Base64 = base64ImageRepresentation,
FileName = Guid.NewGuid().ToString(),
};
Result resp = imagekit.Upload(ob2);
#endregion
#region File Management
// List and search files
GetFileListRequest model = new GetFileListRequest
{
Type = "file",
Limit = 10,
Skip = 0,
};
ResultList res = imagekit.GetFileListRequest(model);
// Get File by FileId
Result res1 = imagekit.GetFileDetail("fileId");
// Delete File by FileId
Result res2 = imagekit.DeleteFile("fileId");
// Bulk Delete
List<string> ob3 = new List<string>();
ob3.Add("fileId-1");
ob3.Add("fileId-2");
ResultFileDelete resultFileDelete = imagekit.BulkDeleteFiles(ob3);
// Copy File
CopyFileRequest cpyRequest = new CopyFileRequest
{
SourceFilePath = "Tst3",
DestinationPath = "Tst3",
IncludeFileVersions = true,
};
ResultNoContent resultNoContent = imagekit.CopyFile(cpyRequest);
// MoveFile
MoveFileRequest moveFile = new MoveFileRequest
{
SourceFilePath = "Tst3",
DestinationPath = "Tst3",
};
ResultNoContent resultNoContentMoveFile = imagekit.MoveFile(moveFile);
// RenameFile
RenameFileRequest renameFileRequest = new RenameFileRequest
{
FilePath = "Tst3",
NewFileName = "Tst4",
PurgeCache = false,
};
ResultRenameFile resultRenameFile = imagekit.RenameFile(renameFileRequest);
#endregion
#region Tags
TagsRequest tagsRequest = new TagsRequest
{
Tags = new List<string>
{
"tag1",
"tag2",
},
FileIds = new List<string>
{
"field1",
},
};
ResultTags resultTags = imagekit.AddTags(tagsRequest);
TagsRequest removeTagsRequest = new TagsRequest
{
Tags = new List<string>
{
"tag1",
"tag2",
},
FileIds = new List<string>
{
"field1",
},
};
ResultTags removeTags = imagekit.RemoveTags(removeTagsRequest);
AiTagsRequest removeAITagsRequest = new AiTagsRequest
{
AiTags = new List<string>
{
"tag1",
"tag2",
},
FileIds = new List<string>
{
"field1",
},
};
ResultTags removeAITags = imagekit.RemoveAiTags(removeAITagsRequest);
#endregion
#region FileVersionRequest
DeleteFileVersionRequest delRequest = new DeleteFileVersionRequest
{
FileId = "fileId",
VersionId = "versionId",
};
ResultNoContent resultNoContent1 = imagekit.DeleteFileVersion(delRequest);
// RestoreFileVersion
Result result = imagekit.RestoreFileVersion("abc", "1");
// GetFileVersions
ResultFileVersions resultFileVersions = imagekit.GetFileVersions("fileId");
// GetFileVersionDetails
ResultFileVersionDetails resultFileVersionDetails = imagekit.GetFileVersionDetails("fileId", "versionId");
#endregion
#region ManageFolder
CreateFolderRequest createFolderRequest = new CreateFolderRequest
{
FolderName = "abc",
ParentFolderPath = "source/folder/path",
};
ResultEmptyBlock resultEmptyBlock = imagekit.CreateFolder(createFolderRequest);
// DeleteFolderRequest
DeleteFolderRequest deleteFolderRequest = new DeleteFolderRequest
{
FolderPath = "source/folder/path/new_folder",
};
ResultNoContent resultNoContent2 = imagekit.DeleteFolder(deleteFolderRequest);
// CopyFolder
CopyFolderRequest cpyFolderRequest = new CopyFolderRequest
{
SourceFolderPath = "Tst3",
DestinationPath = "Tst3",
IncludeFileVersions = true,
};
ResultOfFolderActions resultOfFolderActions = imagekit.CopyFolder(cpyFolderRequest);
// MoveFolder
MoveFolderRequest moveFolderRequest = new MoveFolderRequest
{
SourceFolderPath = "Tst3",
DestinationPath = "Tst3",
};
ResultOfFolderActions resultOfFolderActions1 = imagekit.MoveFolder(moveFolderRequest);
#endregion
#region GetBulkJobStatus
ResultBulkJobStatus resultBulkJobStatus = imagekit.GetBulkJobStatus("jobId");
#endregion
#region Purge
ResultCache resultCache = imagekit.PurgeCache("url");
ResultCacheStatus resultCacheStatus = imagekit.PurgeStatus("requestId");
#endregion
#region Metadata
ResultMetaData resultMetaData = imagekit.GetFileMetadata("fileId");
ResultMetaData resultMetaData1 = imagekit.GetRemoteFileMetadata("https://ik.imagekit.io/demo/medium_cafe_B1iTdD0C.jpg");
// CustomMetaDataFields
ResultCustomMetaDataFieldList resultCustomMetaDataFieldList = imagekit.GetCustomMetaDataFields(true);
// CreateCustomMetaDataFields
CustomMetaDataFieldCreateRequest requestModel = new CustomMetaDataFieldCreateRequest
{
Name = "Tst3",
Label = "Test3",
};
CustomMetaDataFieldSchemaObject schema = new CustomMetaDataFieldSchemaObject
{
Type = CustomMetaDataTypeEnum.Number,
MinValue = 1000,
MaxValue = 3000,
MinLength = 500,
MaxLength = 600,
IsValueRequired = false,
};
requestModel.Schema = schema;
ResultCustomMetaDataField resultCustomMetaDataField = imagekit.CreateCustomMetaDataFields(requestModel);
// UpdateCustomMetaDataFields
CustomMetaDataFieldUpdateRequest requestUpdateModel = new CustomMetaDataFieldUpdateRequest
{
Id = "Tst3",
};
CustomMetaDataFieldSchemaObject updateschema = new CustomMetaDataFieldSchemaObject
{
Type = CustomMetaDataTypeEnum.Number,
MinValue = 1000,
MaxValue = 3000,
MinLength = 500,
MaxLength = 600,
IsValueRequired = false,
};
requestUpdateModel.Schema = schema;
ResultCustomMetaDataField resultCustomMetaDataFieldUpdate = imagekit.UpdateCustomMetaDataFields(requestUpdateModel);
// Delete Custom MetaData
ResultNoContent resultNoContentDel = imagekit.DeleteCustomMetaDataField("id");
#endregion
}
}
}