-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathProgram.cs
More file actions
482 lines (411 loc) · 17.1 KB
/
Program.cs
File metadata and controls
482 lines (411 loc) · 17.1 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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
// <copyright file="Program.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
namespace ImagekitSample
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Net;
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").
Progressive(true).
Lossless(true).
Trim(5).
Metadata(true).
ColorProfile(true).
DefaultImage("folder/file_name.jpg/"). //trailing slash case
Dpr(3).
EffectSharpen(10).
EffectUsm("2-2-0.8-0.024").
EffectContrast(true).
EffectGray().
EffectShadow().
EffectGradient().
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("Generated image URL - {0}", imageUrl);
///// Generating Signed URL
var imgUrl1 = "https://ik.imagekit.io/demo/default-image.jpg";
string[] queryParams = { "b=query", "a=value" };
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
{
file = "http://www.google.com/images/logos/ps_logo2.png",
fileName = "file_name.jpg"
};
Result resp1 = imagekit.Upload(request);
// Upload by bytes
var webClient = new WebClient();
byte[] bytes = webClient.DownloadData("http://www.google.com/images/logos/ps_logo2.png");
FileCreateRequest ob = new FileCreateRequest
{
file = bytes,
fileName = "file_name1.jpg"
};
List<string> tags = new List<string>
{
"Software",
"Developer",
"Engineer"
};
ob.tags = tags;
string customCoordinates = "10,10,20,20";
ob.customCoordinates = customCoordinates;
List<string> responseFields = new List<string>
{
"isPrivateFile",
"tags",
"customCoordinates"
};
ob.responseFields = responseFields;
List<Extension> ext = new List<Extension>();
BackGroundImage bck1 = new BackGroundImage
{
name = "remove-bg",
options = new options()
{ add_shadow = true, semitransparency = false, bg_image_url = "http://www.google.com/images/logos/ps_logo2.png" }
};
AutoTags autoTags = new AutoTags
{
name = "google-auto-tagging",
maxTags = 5,
minConfidence = 95
};
TransformationObject transformationObject = new TransformationObject
{
type = "transformation",
value = "w-100"
};
List<PostTransformation> postTransformations = new List<PostTransformation>();
postTransformations.Add(transformationObject);
UploadTransformation uploadTransformation = new UploadTransformation
{
pre = "l-text,i-Imagekit,fs-50,l-end",
post = postTransformations,
};
ext.Add(bck1);
ext.Add(autoTags);
ob.extensions = ext;
ob.webhookUrl = "https://webhook.site/c78d617f_33bc_40d9_9e61_608999721e2e";
ob.useUniqueFileName = true;
ob.folder = "dummy_folder";
ob.isPrivateFile = false;
ob.overwriteFile = true;
ob.overwriteAITags = true;
ob.overwriteTags = true;
ob.overwriteCustomMetadata = true;
ob.transformation= uploadTransformation;
Result resp2 = imagekit.Upload(ob);
// Get Base64
byte[] bytes1 = webClient.DownloadData("http://www.google.com/images/logos/ps_logo2.png");
byte[] imageArray = bytes1;
string base64ImageRepresentation = Convert.ToBase64String(imageArray);
// Upload by Base64
FileCreateRequest ob2 = new FileCreateRequest
{
file = base64ImageRepresentation,
fileName = Guid.NewGuid().ToString()
};
Result resp = imagekit.Upload(ob2);
#endregion
#region UpdateFile
//Update File Request
FileUpdateRequest updateob = new FileUpdateRequest
{
fileId = "fileId",
};
List<string> updatetags = new List<string>
{
"Software",
"Developer",
"Engineer"
};
updateob.tags = updatetags;
string updatecustomCoordinates = "10,10,20,20";
updateob.customCoordinates = updatecustomCoordinates;
List<string> updateresponseFields = new List<string>
{
"isPrivateFile",
"tags",
"customCoordinates"
};
List<Extension> extModel = new List<Extension>();
BackGroundImage bck = new BackGroundImage
{
name = "remove-bg",
options = new options() { add_shadow = true, semitransparency = false, bg_color = "green" }
};
extModel.Add(bck);
updateob.extensions = extModel;
updateob.webhookUrl = "https://webhook.site/c78d617f_33bc_40d9_9e61_608999721e2e";
Result updateresp = imagekit.UpdateFileDetail(updateob);
#endregion
#region File Management
// List and search files
GetFileListRequest model = new GetFileListRequest
{
Name = "file_name.jpg",
Type = "file",
Limit = 10,
Skip = 0,
Sort = "ASC_CREATED",
SearchQuery = "createdAt >= \"7d\"",
FileType = "image",
Tags = new string[] { "sale", "summer" },
Path = "/"
};
ResultList res = imagekit.GetFileListRequest(model);
// Get File Details
Result res1 = imagekit.GetFileDetail("file_Id");
// Delete File by FileId
ResultDelete res2 = imagekit.DeleteFile("file_Id");
// 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 = "path_1",
destinationPath = "path_2"
};
ResultNoContent resultNoContent = imagekit.CopyFile(cpyRequest);
// MoveFile
MoveFileRequest moveFile = new MoveFileRequest
{
sourceFilePath = "path_1",
destinationPath = "path_2"
};
ResultNoContent resultNoContentMoveFile = imagekit.MoveFile(moveFile);
// RenameFile
RenameFileRequest renameFileRequest = new RenameFileRequest
{
filePath = "path_1",
newFileName = "file_name",
purgeCache = false
};
ResultRenameFile resultRenameFile = imagekit.RenameFile(renameFileRequest);
#endregion
#region Tags
TagsRequest tagsRequest = new TagsRequest
{
tags = new List<string>
{
"tag_1",
"tag_2"
},
fileIds = new List<string>
{
"fileId_1",
},
};
ResultTags resultTags = imagekit.AddTags(tagsRequest);
TagsRequest removeTagsRequest = new TagsRequest
{
tags = new List<string>
{
"tag_1",
"tag_2"
},
fileIds = new List<string>
{
"fileId_1",
},
};
ResultTags removeTags = imagekit.RemoveTags(removeTagsRequest);
AITagsRequest removeAITagsRequest = new AITagsRequest
{
AITags = new List<string>
{
"tag_1",
"tag_2"
},
fileIds = new List<string>
{
"fileId_1",
},
};
ResultTags removeAITags = imagekit.RemoveAITags(removeAITagsRequest);
#endregion
#region FileVersionRequest
DeleteFileVersionRequest delRequest = new DeleteFileVersionRequest
{
fileId = "file_Id",
versionId = "version_Id"
};
ResultNoContent resultNoContent1 = imagekit.DeleteFileVersion(delRequest);
// RestoreFileVersion
Result result = imagekit.RestoreFileVersion("file_Id", "version_Id");
// GetFileVersions
ResultFileVersions resultFileVersions = imagekit.GetFileVersions("file_Id");
// GetFileVersionDetails
ResultFileVersionDetails resultFileVersionDetails = imagekit.GetFileVersionDetails("file_Id", "version_Id");
#endregion
#region ManageFolder
CreateFolderRequest createFolderRequest = new CreateFolderRequest
{
folderName = "folder_name",
parentFolderPath = "source/folder/path"
};
ResultEmptyBlock resultEmptyBlock = imagekit.CreateFolder(createFolderRequest);
// DeleteFolderRequest
DeleteFolderRequest deleteFolderRequest = new DeleteFolderRequest
{
folderPath = "source/folder/path/folder_name",
};
ResultNoContent resultNoContent2 = imagekit.DeleteFolder(deleteFolderRequest);
// CopyFolder
CopyFolderRequest cpyFolderRequest = new CopyFolderRequest
{
sourceFolderPath = "path_1",
destinationPath = "path_2",
includeFileVersions = true
};
ResultOfFolderActions resultOfFolderActions = imagekit.CopyFolder(cpyFolderRequest);
// MoveFolder
MoveFolderRequest moveFolderRequest = new MoveFolderRequest
{
sourceFolderPath = "path_1",
destinationPath = "path_2"
};
ResultOfFolderActions resultOfFolderActions1 = imagekit.MoveFolder(moveFolderRequest);
#endregion
#region GetBulkJobStatus
ResultBulkJobStatus resultBulkJobStatus = imagekit.GetBulkJobStatus("job_Id");
#endregion
#region Purge
ResultCache resultCache = imagekit.PurgeCache("url");
ResultCacheStatus resultCacheStatus = imagekit.PurgeStatus("request_Id");
#endregion
#region Metadata
ResultMetaData resultMetaData = imagekit.GetFileMetadata("file_Id");
ResultMetaData resultMetaData1 = imagekit.GetRemoteFileMetadata("https://ik.imagekit.io/demo/medium_cafe_B1iTdD0C.jpg");
// CustomMetaDataFields
ResultCustomMetaDataFieldList resultCustomMetaDataFieldList = imagekit.GetCustomMetaDataFields(true);
// CreateCustomMetaDataFields
CustomMetaDataFieldCreateRequest requestModelDate = new CustomMetaDataFieldCreateRequest
{
name = "custom_meta_Date",
label = "TestmetaDat"
};
CustomMetaDataFieldSchemaObject schemaDate = new CustomMetaDataFieldSchemaObject
{
type = CustomMetaDataTypeEnum.Date.ToString(),
minValue = "2022-11-30T10:11:10+00:00",
maxValue = "2022-12-30T10:11:10+00:00",
isValueRequired = true,
defaultValue = "2022-12-30T10:11:10+00:00"
};
requestModelDate.schema = schemaDate;
ResultCustomMetaDataField resultCustomMetaDataFieldDate = imagekit.CreateCustomMetaDataFields(requestModelDate);
CustomMetaDataFieldCreateRequest requestModel = new CustomMetaDataFieldCreateRequest
{
name = "custom_meta_1",
label = "Testmeta"
};
CustomMetaDataFieldSchemaObject schema = new CustomMetaDataFieldSchemaObject
{
type = CustomMetaDataTypeEnum.Number.ToString(),
minValue = 2000,
maxValue = 3000,
isValueRequired = true,
defaultValue = 2500
};
requestModel.schema = schema;
ResultCustomMetaDataField resultCustomMetaDataField1 = imagekit.CreateCustomMetaDataFields(requestModel);
CustomMetaDataFieldCreateRequest requestModel1 = new CustomMetaDataFieldCreateRequest
{
name = "custom_meta_2",
label = "Testmeta"
};
CustomMetaDataFieldSchemaObject schema1 = new CustomMetaDataFieldSchemaObject
{
type = CustomMetaDataTypeEnum.Text.ToString(),
minLength = 1000,
maxLength = 2000,
isValueRequired = true,
defaultValue = "2500"
};
requestModel1.schema = schema1;
ResultCustomMetaDataField resultCustomMetaDataField = imagekit.CreateCustomMetaDataFields(requestModel1);
CustomMetaDataFieldCreateRequest requestModelSelect = new CustomMetaDataFieldCreateRequest
{
name = "custom_meta_Select1",
label = "TestmetaSelect1"
};
CustomMetaDataFieldSchemaObject schemaSelect = new CustomMetaDataFieldSchemaObject
{
type = CustomMetaDataTypeEnum.SingleSelect.ToString(),
selectOptions = new string[] { "small", "medium", "large" },
isValueRequired = true,
defaultValue = "medium"
};
requestModelSelect.schema = schemaSelect;
ResultCustomMetaDataField resultCustomMetaDataFieldSelect = imagekit.CreateCustomMetaDataFields(requestModelSelect);
// UpdateCustomMetaDataFields
CustomMetaDataFieldUpdateRequest requestUpdateModel = new CustomMetaDataFieldUpdateRequest
{
Id = "field_Id",
};
CustomMetaDataFieldSchemaObject updateschema = new CustomMetaDataFieldSchemaObject
{
type = "Number",
minValue = 300,
maxValue = 500
};
requestUpdateModel.schema = updateschema;
ResultCustomMetaDataField resultCustomMetaDataFieldUpdate = imagekit.UpdateCustomMetaDataFields(requestUpdateModel);
//Delete Custom MetaData
ResultNoContent resultNoContentDel = imagekit.DeleteCustomMetaDataField("field_id");
// Get Authentication Token
var authenticationParameters = imagekit.GetAuthenticationParameters("your_token");
Console.WriteLine("Authentication Parameters: {0}", JToken.FromObject(authenticationParameters).ToString());
#endregion
}
}
}