-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
1058 lines (983 loc) · 42.5 KB
/
Program.cs
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Copyright (c) 2017 Autodesk, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// by Cyrille Fauvel
// Autodesk Forge Partner Development
// January 2017
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// https://www.nuget.org/packages/CommandLineParser/2.1.1-beta
// https://github.com/gsscoder/commandline/wiki/Latest-Version#verbs
using CommandLine;
using CommandLine.Text;
using Autodesk.Forge;
using Autodesk.Forge.Client;
using System.Collections.Specialized;
using System.Text.RegularExpressions;
using Autodesk.Forge.Model;
using System.IO;
using Newtonsoft.Json.Linq;
namespace forge.commandline_csharp {
#region Command Definitions
[Verb ("2legged", HelpText = "get an application access token (2 legged)")]
class TwoLeggedOptions {
}
[Verb ("3legged", HelpText = "get an user access token (3 legged)")]
class ThreeLeggedOptions {
//[Option('c', "code", SetName = "code", HelpText = "code returned to you after user authentication")]
[Value(0, HelpText = "code returned to you after user authentication")]
public string code { get; set; }
}
[Verb ("aboutme", HelpText = "3legged aboutme information")]
class AboutMeOptions {
}
[Verb ("buckets", HelpText = "list local/server buckets")]
class BucketsOptions {
[Option('s', "server", SetName = "server", Default = false, HelpText = "list from server vs local")]
public bool server { get; set; }
[Option('a', "startAt", SetName = "startAt", HelpText = "startAt: where to start in the list [string, default: none]")]
public string startAt { get; set; }
[Option('l', "limit", SetName = "limit", Default = 10, HelpText = "limit: how many to return [integer, default: 10]")]
public int limit { get; set; }
[Option('r', "region", SetName = "region", Default = "US", HelpText = "region: US or EMEA [string, default: US]")]
public string region { get; set; }
}
[Verb ("bucket", HelpText = "set or get the current bucket")]
class BucketOptions {
[Value(0, HelpText = "bucket name")]
public string bucket { get; set; }
}
[Verb ("bucketCreate", HelpText = "create a new bucket,; default Type is Transient, values can be Transient/Temporary/Persistent")]
class BucketCreateOptions {
[Value(0, HelpText = "bucket name")]
public string bucket { get; set; }
[Option('r', "region", SetName = "region", Default = "US", HelpText = "region: US or EMEA [string, default: US]")]
public string region { get; set; }
[Option('t', "type", SetName = "type", Default = PostBucketsPayload.PolicyKeyEnum.Transient, HelpText = "type: Transient or Temporary or Persistent [string, default: Transient]")]
public PostBucketsPayload.PolicyKeyEnum type { get; set; }
}
[Verb ("bucketCheck", HelpText = "check bucket validity, outputs the expiration; date/time for this bucket; if no parameter use the current bucket")]
class BucketCheckOptions {
[Value(0, HelpText = "bucket name")]
public string bucket { get; set; }
}
[Verb ("bucketItems", HelpText = "list items in a given bucket; required to be in the API white list to use this API; if no parameter use the current bucket")]
class BucketItemsOptions {
[Value(0, HelpText = "bucket name")]
public string bucket { get; set; }
[Option('a', "startAt", SetName = "startAt", HelpText = "startAt: where to start in the list [string, default: none]")]
public string startAt { get; set; }
[Option('l', "limit", SetName = "limit", Default = 10, HelpText = "limit: how many to return [integer, default: 10]")]
public int limit { get; set; }
[Option('r', "region", SetName = "region", Default = "US", HelpText = "region: US or EMEA [string, default: US]")]
public string region { get; set; }
}
[Verb ("bucketCheck", HelpText = "delete a given bucket; if no parameter delete the current bucket")]
class BucketDeleteOptions {
[Value(0, HelpText = "bucket name")]
public string bucket { get; set; }
}
[Verb ("upload", HelpText = "upload a file in the current bucket")]
class UploadOptions {
[Value(0, HelpText = "file name")]
public string filename { get; set; }
}
[Verb ("resumable", HelpText = "upload a file in multiple pieces (i.e. resumables)")]
class ResumableOptions {
[Value(0, HelpText = "file name")]
public string filename { get; set; }
[Value(1, HelpText = "split the file in N pieces")]
public int pieces { get; set; }
}
[Verb ("download", HelpText = "download the file from OSS")]
class DownloadOptions {
[Value(0, HelpText = "file key")]
public string fileKey { get; set; }
[Value(1, HelpText = "output file name")]
public string outputFile { get; set; }
[Option('f', "file", SetName = "file", Default = false, HelpText = "fileKey represent the final objectKey on OSS vs a local fileKey")]
public bool file { get; set; }
}
[Verb ("objectDetails", HelpText = "file information")]
class ObjectDetailsOptions {
[Value(0, HelpText = "file key")]
public string fileKey { get; set; }
[Option('f', "file", SetName = "file", Default = false, HelpText = "fileKey represent the final objectKey on OSS vs a local fileKey")]
public bool file { get; set; }
}
[Verb ("translate", HelpText = "list items in a given bucket; required to be in the API white list to use this API; if no parameter use the current bucket")]
class TranslateOptions {
[Value(0, HelpText = "file key")]
public string fileKey { get; set; }
[Option('f', "file", SetName = "file", Default = false, HelpText = "fileKey represent the final objectKey on OSS vs a local fileKey")]
public bool file { get; set; }
[Option('e', "entry", SetName = "entry", HelpText = "rootFile: which file to start from")]
public string entry { get; set; }
}
[Verb ("translateProgress", HelpText = "file translation progress")]
class TranslateProgressOptions {
[Value(0, HelpText = "file key")]
public string fileKey { get; set; }
[Option('f', "file", SetName = "file", Default = false, HelpText = "fileKey represent the final objectKey on OSS vs a local fileKey")]
public bool file { get; set; }
}
[Verb ("manifest", HelpText = "file manifest")]
class ManifestOptions {
[Value(0, HelpText = "file key")]
public string fileKey { get; set; }
[Option('f', "file", SetName = "file", Default = false, HelpText = "fileKey represent the final objectKey on OSS vs a local fileKey")]
public bool file { get; set; }
}
[Verb ("metadata", HelpText = "file metadata")]
class MetadataOptions {
[Value(0, HelpText = "file key")]
public string fileKey { get; set; }
[Option('f', "file", SetName = "file", Default = false, HelpText = "fileKey represent the final objectKey on OSS vs a local fileKey")]
public bool file { get; set; }
}
[Verb ("thumbnail", HelpText = "get thumbnail")]
class ThumbnailOptions {
[Value(0, HelpText = "file key")]
public string fileKey { get; set; }
[Value(1, HelpText = "output file name")]
public string outputFile { get; set; }
[Option('f', "file", SetName = "file", Default = false, HelpText = "fileKey represent the final objectKey on OSS vs a local fileKey")]
public bool file { get; set; }
}
[Verb ("deleteFile", HelpText = "delete the source file from the bucket")]
class DeleteFileOptions {
[Value(0, HelpText = "file key")]
public string fileKey { get; set; }
[Option('f', "file", SetName = "file", Default = false, HelpText = "fileKey represent the final objectKey on OSS vs a local fileKey")]
public bool file { get; set; }
}
[Verb ("deleteManifest", HelpText = "delete the manifest and all its translated output files (derivatives)")]
class DeleteManifestOptions {
[Value(0, HelpText = "file key")]
public string fileKey { get; set; }
[Option('f', "file", SetName = "file", Default = false, HelpText = "fileKey represent the final objectKey on OSS vs a local fileKey")]
public bool file { get; set; }
}
[Verb ("html", HelpText = "generate default html page")]
class HtmlOptions {
[Value(0, HelpText = "file key")]
public string fileKey { get; set; }
[Value(1, HelpText = "output file name")]
public string outputFile { get; set; }
[Option('f', "file", SetName = "file", Default = false, HelpText = "fileKey represent the final objectKey on OSS vs a local fileKey")]
public bool file { get; set; }
}
#endregion
class Program {
static void Main (string [] args) {
// Limited to 16
//Parser.Default.ParseArguments<
// TwoLeggedOptions,
// ThreeLeggedOptions
// > (args)
// .MapResult (
// (TwoLeggedOptions opts) => RunTwoLegged (opts),
// (ThreeLeggedOptions opts) => RunThreeLegged (opts),
// errs => 1
// ) ;
Parser.Default.ParseArguments (args, new [] {
typeof (TwoLeggedOptions),
typeof (ThreeLeggedOptions),
typeof (AboutMeOptions),
typeof (BucketsOptions),
typeof (BucketOptions),
typeof (BucketCreateOptions),
typeof (BucketCheckOptions),
typeof (BucketItemsOptions),
typeof (BucketDeleteOptions),
typeof (UploadOptions),
typeof (ResumableOptions),
typeof (DownloadOptions),
typeof (ObjectDetailsOptions),
typeof (TranslateOptions),
typeof (TranslateProgressOptions),
typeof (ManifestOptions),
typeof (MetadataOptions),
typeof (ThumbnailOptions),
typeof (DeleteFileOptions),
typeof (DeleteManifestOptions),
typeof (HtmlOptions)
})
.WithParsed<TwoLeggedOptions> (opts => RunTwoLegged (opts))
.WithParsed<ThreeLeggedOptions> (opts => RunThreeLegged (opts))
.WithParsed<AboutMeOptions> (opts => RunAboutMe (opts))
.WithParsed<BucketsOptions> (opts => RunBuckets (opts))
.WithParsed<BucketOptions> (opts => RunBucket (opts))
.WithParsed<BucketCreateOptions> (opts => RunBucketCreate (opts))
.WithParsed<BucketCheckOptions> (opts => RunBucketCheck (opts))
.WithParsed<BucketItemsOptions> (opts => RunBucketItems (opts))
.WithParsed<BucketDeleteOptions> (opts => RunBucketDelete (opts))
.WithParsed<UploadOptions> (opts => RunUpload (opts))
.WithParsed<ResumableOptions> (opts => RunResumable (opts))
.WithParsed<DownloadOptions> (opts => RunDownload (opts))
.WithParsed<ObjectDetailsOptions> (opts => RunObjectDetails (opts))
.WithParsed<TranslateOptions> (opts => RunTranslate (opts))
.WithParsed<TranslateProgressOptions> (opts => RunTranslateProgress (opts))
.WithParsed<ManifestOptions> (opts => RunManifest (opts))
.WithParsed<MetadataOptions> (opts => RunMetadata (opts))
.WithParsed<ThumbnailOptions> (opts => RunThumbnail (opts))
.WithParsed<DeleteFileOptions> (opts => RunDeleteFile (opts))
.WithParsed<DeleteManifestOptions> (opts => RunDeleteManifest (opts))
.WithParsed<HtmlOptions> (opts => RunHtml (opts))
.WithNotParsed (errs => {}) ;
}
#region Initialization
private static string FORGE_CLIENT_ID ="" ; // 'your_client_id'
private static string FORGE_CLIENT_SECRET ="" ; // 'your_client_secret'
private static string PORT ="" ; // 3006
private static string FORGE_CALLBACK =null ; // 'http://localhost:' + PORT + '/oauth' ;
private static string grantType ="client_credentials" ; // {String} Must be ``client_credentials``
private static Scope[] scope =new Scope[] { Scope.DataRead, Scope.DataWrite, Scope.DataCreate,
Scope.DataSearch, Scope.BucketCreate, Scope.BucketRead, Scope.BucketUpdate, Scope.BucketDelete } ;
private static Scope[] scopeViewer =new Scope[] { Scope.DataRead } ;
internal static bool readKeys () {
FORGE_CLIENT_ID =Environment.GetEnvironmentVariable ("FORGE_CLIENT_ID") ;
FORGE_CLIENT_SECRET =Environment.GetEnvironmentVariable ("FORGE_CLIENT_SECRET") ;
PORT =Environment.GetEnvironmentVariable ("PORT") ;
if ( PORT == null )
PORT ="3006" ;
if ( FORGE_CALLBACK == null ) {
FORGE_CALLBACK =Environment.GetEnvironmentVariable ("FORGE_CALLBACK") ;
if ( FORGE_CALLBACK == null )
FORGE_CALLBACK ="http://localhost:" + PORT + "/oauth" ;
}
return (true) ;
}
#endregion
#region Access Token
// One example for async implementation - do not use in a console application
internal async static Task<ApiResponse<dynamic>> oauthExecAsync () {
TwoLeggedApi apiInstance =new TwoLeggedApi () ;
ApiResponse<dynamic> bearer =null ;
try {
readKeys () ;
bearer =await apiInstance.AuthenticateAsyncWithHttpInfo (FORGE_CLIENT_ID, FORGE_CLIENT_SECRET, grantType, scope) ;
if ( httpErrorHandler (bearer, "Failed to get your token", false) ) {
System.IO.File.Delete ("data/access_token") ;
return (null) ;
}
//Console.WriteLine (bearer.Data.ToString () as string) ;
string token =bearer.Data.token_type + " " + bearer.Data.access_token ;
Console.WriteLine ("Your new 2-legged access token is: " + token) ;
DateTime dt =DateTime.Now ;
dt.AddSeconds (double.Parse (bearer.Data.expires_in)) ;
Console.WriteLine ("Expires at: " + dt.ToLocalTime ()) ;
System.IO.File.WriteAllText ("/data/access_token", token) ;
return (bearer) ;
} catch ( Exception ex ) {
Console.Error.WriteLine ("Exception when calling TwoLeggedApi.Authenticate: " + ex.Message) ;
return (null) ;
}
}
internal static ApiResponse<dynamic> oauthExec (bool bEcho =true) {
try {
readKeys () ;
TwoLeggedApi apiInstance =new TwoLeggedApi () ;
ApiResponse<dynamic> bearer =apiInstance.AuthenticateWithHttpInfo (FORGE_CLIENT_ID, FORGE_CLIENT_SECRET, grantType, scope) ;
if ( httpErrorHandler (bearer, "Failed to get your token", false) ) {
System.IO.File.Delete ("data/access_token") ;
return (null) ;
}
//Console.WriteLine (bearer.Data.ToString () as string) ;
string token =bearer.Data.token_type + " " + bearer.Data.access_token ;
if ( bEcho )
Console.WriteLine ("Your new 2-legged access token is: " + token) ;
DateTime dt =DateTime.Now ;
dt.AddSeconds (double.Parse (bearer.Data.expires_in.ToString ())) ;
if ( bEcho )
Console.WriteLine ("Expires at: " + dt.ToLocalTime ()) ;
System.IO.File.WriteAllText ("data/access_token", token) ;
return (bearer) ;
} catch ( Exception ex ) {
Console.Error.WriteLine ("Exception when calling TwoLeggedApi.AuthenticateWithHttpInfo: " + ex.Message) ;
return (null) ;
}
}
internal static string access_token (bool bStripType =true) {
string st =System.IO.File.ReadAllText ("data/access_token") ;
if ( bStripType )
st =st.Split (new char [] { ' ' }) [1] ;
return (st) ;
}
internal static string readOnlyAccessToken () {
try {
readKeys () ;
TwoLeggedApi apiInstance =new TwoLeggedApi () ;
ApiResponse<dynamic> bearer =apiInstance.AuthenticateWithHttpInfo (FORGE_CLIENT_ID, FORGE_CLIENT_SECRET, grantType, scopeViewer) ;
httpErrorHandler (bearer, "Failed to get your token") ;
//Console.WriteLine (bearer.Data.ToString () as string) ;
return (bearer.Data.access_token) ;
} catch ( Exception ex ) {
Console.Error.WriteLine ("Exception when calling TwoLeggedApi.AuthenticateWithHttpInfo: " + ex.Message) ;
return (null) ;
}
}
#endregion
#region Commands
internal static int RunTwoLegged (TwoLeggedOptions opts) {
ApiResponse<dynamic> bearer =oauthExec () ;
return (0) ;
}
internal static int RunThreeLegged (ThreeLeggedOptions opts) {
ThreeLeggedApi oa3Legged =new ThreeLeggedApi () ;
readKeys () ;
if ( string.IsNullOrEmpty (opts.code) ) {
string uri =oa3Legged.Authorize (FORGE_CLIENT_ID, "code", FORGE_CALLBACK, scope) ;
System.Diagnostics.Process.Start (uri) ;
Console.WriteLine ("Wait for the browser to return a code and run this script again with the code as parameter...") ;
return (0) ;
} else {
try {
ApiResponse<dynamic> bearer =oa3Legged.GettokenWithHttpInfo (FORGE_CLIENT_ID, FORGE_CLIENT_SECRET, "authorization_code", opts.code, FORGE_CALLBACK) ;
if ( httpErrorHandler (bearer, "Failed to get your token", false) ) {
System.IO.File.Delete ("data/access_token") ;
return (2) ;
}
//Console.WriteLine (bearer.Data.ToString () as string) ;
string token =bearer.Data.token_type + " " + bearer.Data.access_token ;
Console.WriteLine ("Your new 3-legged access token is: " + token) ;
DateTime dt =DateTime.Now ;
dt.AddSeconds (double.Parse (bearer.Data.expires_in.ToString ())) ;
Console.WriteLine ("Expires at: " + dt.ToLocalTime ()) ;
System.IO.File.WriteAllText ("data/access_token", token) ;
token =bearer.Data.token_type + " " + bearer.Data.refresh_token ;
System.IO.File.WriteAllText ("data/refresh_token", token) ;
} catch ( Exception ex ) {
Console.Error.WriteLine ("Exception when calling ThreeLeggedApi.Gettoken: " + ex.Message) ;
return (1) ;
}
}
return (0) ;
}
internal static int RunAboutMe (AboutMeOptions opts) {
try {
Console.WriteLine ("About Me!...") ;
InformationalApi oa3Info =new InformationalApi () ;
oa3Info.Configuration.AccessToken =access_token () ;
ApiResponse<dynamic> response =oa3Info.AboutMeWithHttpInfo () ;
httpErrorHandler (response, "Failed to access user information") ;
Console.WriteLine (response.Data.ToString () as string) ;
} catch ( Exception ex ) {
Console.Error.WriteLine ("Exception when calling InformationalApi.AboutMeWithHttpInfo: " + ex.Message) ;
return (2) ;
}
return (0) ;
}
internal static int RunBuckets (BucketsOptions opts) {
if ( opts.server ) {
Console.WriteLine ("Listing from " + opts.startAt + " to " + opts.limit) ;
try {
BucketsApi ossBuckets =new BucketsApi () ;
ossBuckets.Configuration.AccessToken =access_token () ;
ApiResponse<dynamic> response =ossBuckets.GetBucketsWithHttpInfo (opts.region, opts.limit, opts.startAt) ;
httpErrorHandler (response, "Failed to access buckets list") ;
Console.WriteLine (response.Data.ToString () as string) ;
if ( !hasOwnProperty (response.Data, "next") ) {
Console.WriteLine ("Your search is complete, no more items to list") ;
} else {
var uri =new Uri (response.Data.next) ;
NameValueCollection url_parts =System.Web.HttpUtility.ParseQueryString (uri.Query) ;
string startAt =url_parts ["startAt"] ;
Console.WriteLine ("Your next search startAt is: " + startAt) ;
}
} catch ( Exception ex ) {
Console.Error.WriteLine ("Exception when calling BucketsApi.GetBucketsWithHttpInfo: " + ex.Message) ;
return (2) ;
}
} else {
string[] files =System.IO.Directory.GetFiles ("data", "*.bucket.json") ;
if ( files.Length == 0 )
Console.WriteLine ("0 file in folder 'data'") ;
foreach ( string fn in files )
Console.WriteLine (fn) ;
}
return (0) ;
}
internal static int RunBucket (BucketOptions opts) {
if ( string.IsNullOrEmpty (opts.bucket) ) {
string data =System.IO.File.ReadAllText ("data/bucket") ;
Console.Write ("Current bucket is: " + data) ;
} else {
if ( !checkBucketKey (opts.bucket) )
return (3) ;
System.IO.File.WriteAllText ("data/bucket", opts.bucket) ;
Console.Write ("Your current bucket is now: " + opts.bucket) ;
}
return (0) ;
}
internal static int RunBucketCreate (BucketCreateOptions opts) {
if ( !checkBucketKey (opts.bucket) )
return (2) ;
try {
Console.WriteLine ("Create bucket: " + opts.bucket) ;
BucketsApi ossBuckets =new BucketsApi () ;
ossBuckets.Configuration.AccessToken =access_token () ;
PostBucketsPayload payload =new PostBucketsPayload (opts.bucket, null, opts.type) ;
ApiResponse<dynamic> response =ossBuckets.CreateBucketWithHttpInfo (payload, opts.region) ;
httpErrorHandler (response, "Failed to create bucket") ;
System.IO.File.WriteAllText ("data/bucket", opts.bucket) ;
System.IO.File.WriteAllText ("data/" + opts.bucket + ".bucket.json", response.Data.ToString () as string) ;
Console.WriteLine ("bucket created") ;
} catch ( Exception ex ) {
Console.Error.WriteLine ("Exception when calling BucketsApi.CreateBucketWithHttpInfo: " + ex.Message) ;
return (2) ;
}
return (0) ;
}
internal static int RunBucketCheck (BucketCheckOptions opts) {
string bucketKey =currentBucket (opts.bucket) ;
if ( !checkBucketKey (bucketKey) )
return (2) ;
try {
Console.WriteLine ("Getting bucket details") ;
BucketsApi ossBuckets =new BucketsApi () ;
ossBuckets.Configuration.AccessToken =access_token () ;
ApiResponse<dynamic> response =ossBuckets.GetBucketDetailsWithHttpInfo (bucketKey) ;
httpErrorHandler (response, "Failed to access bucket details") ;
if ( response.Data.policyKey == "transient" ) // 24 hours
Console.WriteLine ("bucket content will expire after: 24 hours") ;
else if ( response.Data.policyKey == "temporary" ) // 30 days
Console.WriteLine ("bucket content will expire after: 30 days") ;
else // persistent
Console.WriteLine ("bucket content will never expire") ;
} catch ( Exception ex ) {
Console.Error.WriteLine ("Exception when calling BucketsApi.GetBucketDetailsWithHttpInfo: " + ex.Message) ;
return (2) ;
}
return (0) ;
}
internal static int RunBucketItems (BucketItemsOptions opts) {
string bucketKey =currentBucket (opts.bucket) ;
if ( !checkBucketKey (bucketKey) )
return (2) ;
try {
Console.WriteLine ("Listing bucket " + bucketKey + " content") ;
ObjectsApi ossObjects =new ObjectsApi () ;
ossObjects.Configuration.AccessToken =access_token () ;
ApiResponse<dynamic> response =ossObjects.GetObjectsWithHttpInfo (bucketKey, opts.limit, null, opts.startAt) ;
httpErrorHandler (response, "Failed to access buckets list") ;
Console.WriteLine (response.Data.ToString () as string) ;
if ( !hasOwnProperty (response.Data, "next") ) {
Console.WriteLine ("Your search is complete, no more items to list") ;
} else {
var uri =new Uri (response.Data.next) ;
NameValueCollection url_parts =System.Web.HttpUtility.ParseQueryString (uri.Query) ;
string startAt =url_parts ["startAt"] ;
Console.WriteLine ("Your next search startAt is: " + startAt) ;
}
} catch ( Exception ex ) {
Console.Error.WriteLine ("Exception when calling ObjectsApi.GetObjectsWithHttpInfo: " + ex.Message) ;
return (2) ;
}
return (0) ;
}
internal static int RunBucketDelete (BucketDeleteOptions opts) {
string bucketKey =currentBucket (opts.bucket) ;
if ( !checkBucketKey (bucketKey) )
return (2) ;
try {
Console.WriteLine ("Delete bucket: " + bucketKey) ;
BucketsApi ossBuckets =new BucketsApi () ;
ossBuckets.Configuration.AccessToken =access_token () ;
ApiResponse<dynamic> response =ossBuckets.DeleteBucketWithHttpInfo (bucketKey) ;
httpErrorHandler (response, "Failed to delete bucket", false) ;
System.IO.File.Delete ("data/bucket") ;
System.IO.File.Delete ("data/" + bucketKey + ".bucket.json") ;
Console.WriteLine ("bucket deleted") ;
} catch ( Exception ex ) {
Console.Error.WriteLine ("Exception when calling BucketsApi.DeleteBucketWithHttpInfo: " + ex.Message) ;
return (2) ;
}
return (0) ;
}
internal static int RunUpload (UploadOptions opts) {
string bucketKey =currentBucket (string.Empty) ;
if ( !checkBucketKey (bucketKey) )
return (2) ;
try {
string fileKey =System.IO.Path.GetFileName (opts.filename) ;
if ( !System.IO.File.Exists (opts.filename) ) {
Console.WriteLine (opts.filename + " does not exists (or not found)") ;
return (2) ;
}
ObjectsApi ossObjects =new ObjectsApi () ;
ossObjects.Configuration.AccessToken =access_token () ;
Console.WriteLine ("Uploading file: " + opts.filename) ;
using ( StreamReader streamReader =new StreamReader (opts.filename) ) {
ApiResponse<dynamic> response =ossObjects.UploadObjectWithHttpInfo (bucketKey,
fileKey, (int)streamReader.BaseStream.Length, streamReader.BaseStream,
"application/octet-stream") ;
httpErrorHandler (response, "Failed to upload file") ;
System.IO.File.WriteAllText ("data/" + bucketKey + "." + fileKey + ".json", response.Data.ToString () as string) ;
Console.WriteLine ("Upload successful") ;
Console.WriteLine ("ID: " + response.Data.objectId) ;
Console.WriteLine ("URN: " + Base64Encode (response.Data.objectId)) ;
Console.WriteLine ("Location: " + response.Data.location) ;
}
} catch ( Exception ex ) {
Console.Error.WriteLine ("Exception when calling ObjectsApi.UploadObject: " + ex.Message) ;
return (2) ;
}
return (0) ;
}
internal static int RunResumable (ResumableOptions opts) {
string bucketKey =currentBucket (string.Empty) ;
if ( !checkBucketKey (bucketKey) )
return (2) ;
try {
string fileKey =System.IO.Path.GetFileName (opts.filename) ;
if ( !System.IO.File.Exists (opts.filename) ) {
Console.WriteLine (opts.filename + " does not exists (or not found)") ;
return (2) ;
}
long size =new System.IO.FileInfo (opts.filename).Length ;
long pieceSz =size / opts.pieces ;
long modSz=size % opts.pieces ;
if ( modSz > 0 )
opts.pieces++ ;
Console.WriteLine ("Uploading file: " + Path.GetFileName (opts.filename) + " in " + opts.pieces + " pieces") ;
using ( FileStream streamReader =new FileStream (opts.filename, FileMode.Open) ) {
string sessionId =RandomString (12) ;
ObjectsApi ossObjects =new ObjectsApi () ;
for ( int i =0 ; i < opts.pieces ; i++ ) {
long start =i * pieceSz ;
long end =Math.Min (size, (i + 1) * pieceSz) - 1 ;
string range ="bytes " + start + "-" + end + "/" + size ;
long length =end - start + 1 ;
Console.WriteLine ("Loading " + range) ;
byte [] buffer =new byte [length] ;
MemoryStream memoryStream =new MemoryStream (buffer) ;
int nb =streamReader.Read (buffer, 0, (int)length) ;
//memoryStream.Seek (0, SeekOrigin.Begin) ;
memoryStream.Write (buffer, 0, nb) ;
memoryStream.Position =0 ;
ApiResponse<dynamic> bearer =oauthExec (false) ;
ossObjects.Configuration.AccessToken =bearer.Data.access_token ;
ApiResponse<dynamic> response =ossObjects.UploadChunkWithHttpInfo (bucketKey,
fileKey, (int)length, range, sessionId, memoryStream,
"application/octet-stream") ;
httpErrorHandler (response, "Failed to upload partial file") ;
if ( response.StatusCode == 202 ) {
Console.WriteLine ("Partial upload accepted") ;
continue ;
}
System.IO.File.WriteAllText ("data/" + bucketKey + "." + fileKey + ".json", response.Data.ToString () as string) ;
Console.WriteLine ("Upload successful") ;
Console.WriteLine ("ID: " + response.Data.objectId) ;
Console.WriteLine ("URN: " + Base64Encode (response.Data.objectId)) ;
Console.WriteLine ("Location: " + response.Data.location) ;
}
}
} catch ( Exception ex ) {
Console.Error.WriteLine ("Exception when calling ObjectsApi.UploadChunkWithHttpInfo: " + ex.Message) ;
return (2) ;
}
return (0) ;
}
internal static int RunDownload (DownloadOptions opts) {
string bucketKey =currentBucket (string.Empty) ;
if ( !checkBucketKey (bucketKey) )
return (2) ;
try {
if ( opts.file )
opts.fileKey =readFileKey (bucketKey, opts.fileKey) ;
Console.WriteLine ("Downloading file: " + opts.fileKey) ;
ObjectsApi ossObjects =new ObjectsApi () ;
ossObjects.Configuration.AccessToken =access_token () ;
ApiResponse<dynamic> response =ossObjects.GetObjectWithHttpInfo (bucketKey, opts.fileKey) ;
httpErrorHandler (response, "Failed to download file") ;
Stream downloadObj =response.Data as Stream ;
downloadObj.Position =0 ;
//using ( BinaryReader sr =new BinaryReader (downloadObj) )
using ( FileStream outputFile =new FileStream (opts.outputFile, FileMode.Create) )
downloadObj.CopyTo (outputFile) ;
Console.WriteLine ("Download successful") ;
} catch ( Exception ex ) {
Console.Error.WriteLine ("Exception when calling ObjectsApi.GetObjectWithHttpInfo: " + ex.Message) ;
return (2) ;
}
return (0) ;
}
internal static int RunObjectDetails (ObjectDetailsOptions opts) {
string bucketKey =currentBucket (string.Empty) ;
if ( !checkBucketKey (bucketKey) )
return (2) ;
try {
if ( opts.file )
opts.fileKey =readFileKey (bucketKey, opts.fileKey) ;
Console.WriteLine ("Getting details for file: " + opts.fileKey) ;
ObjectsApi ossObjects =new ObjectsApi () ;
ossObjects.Configuration.AccessToken =access_token () ;
ApiResponse<dynamic> response =ossObjects.GetObjectDetailsWithHttpInfo (bucketKey, opts.fileKey) ;
httpErrorHandler (response, "Failed to download file") ;
Console.WriteLine (response.Data.ToString () as string) ;
System.IO.File.WriteAllText ("data/" + bucketKey + "." + opts.fileKey + ".json", response.Data.ToString () as string) ;
} catch ( Exception ex ) {
Console.Error.WriteLine ("Exception when calling ObjectsApi.GetObjectDetailsWithHttpInfo: " + ex.Message) ;
return (2) ;
}
return (0) ;
}
internal static int RunTranslate (TranslateOptions opts) {
var bucketKey =currentBucket (string.Empty) ;
if ( !checkBucketKey (bucketKey) )
return (2) ;
try {
if ( opts.file )
opts.fileKey =readFileKey (bucketKey, opts.fileKey) ;
Console.WriteLine ("Request file to be translated") ;
string urn =URN (bucketKey, opts.fileKey, false) ;
if ( opts.entry == null )
opts.entry =opts.fileKey ;
JobPayloadInput jobInput =new JobPayloadInput (
urn,
Path.GetExtension (opts.fileKey).ToLower () == ".zip",
opts.entry
) ;
JobPayloadOutput jobOutput =new JobPayloadOutput (
new List<JobPayloadItem> (
new JobPayloadItem [] {
new JobPayloadItem (
JobPayloadItem.TypeEnum.Svf,
new List<JobPayloadItem.ViewsEnum> (
new JobPayloadItem.ViewsEnum [] {
JobPayloadItem.ViewsEnum._2d, JobPayloadItem.ViewsEnum._3d }
),
null
)}
)) ;
JobPayload job =new JobPayload (jobInput, jobOutput) ;
bool bForce =true ;
DerivativesApi md =new DerivativesApi () ;
md.Configuration.AccessToken =access_token () ;
ApiResponse<dynamic> response =md.TranslateWithHttpInfo (job, bForce) ;
httpErrorHandler (response, "Failed to register file for translation") ;
Console.WriteLine ("Registration successfully submitted.") ;
Console.WriteLine (response.Data.ToString () as string) ;
} catch ( Exception ex ) {
Console.Error.WriteLine ("Exception when calling DerivativesApi.TranslateWithHttpInfo: " + ex.Message) ;
return (2) ;
}
return (0) ;
}
internal static int RunTranslateProgress (TranslateProgressOptions opts) {
string bucketKey =currentBucket (string.Empty) ;
if ( !checkBucketKey (bucketKey) )
return (2) ;
try {
if ( opts.file )
opts.fileKey =readFileKey (bucketKey, opts.fileKey) ;
Console.WriteLine ("Checking file translation progress") ;
string urn =URN (bucketKey, opts.fileKey) ;
DerivativesApi md =new DerivativesApi () ;
md.Configuration.AccessToken =access_token () ;
ApiResponse<dynamic> response =md.GetManifestWithHttpInfo (urn) ;
httpErrorHandler (response, "Failed to get file manifest") ;
Console.WriteLine ("Request: " + response.Data.status + " (" + response.Data.progress + ")") ;
} catch ( Exception ex ) {
Console.Error.WriteLine ("Exception when calling DerivativesApi.GetManifestWithHttpInfo: " + ex.Message) ;
return (2) ;
}
return (0) ;
}
internal static int RunManifest (ManifestOptions opts) {
string bucketKey =currentBucket (string.Empty) ;
if ( !checkBucketKey (bucketKey) )
return (2) ;
try {
if ( opts.file )
opts.fileKey =readFileKey (bucketKey, opts.fileKey) ;
Console.WriteLine ("Getting file manifest") ;
string urn =URN (bucketKey, opts.fileKey) ;
DerivativesApi md =new DerivativesApi () ;
md.Configuration.AccessToken =access_token () ;
ApiResponse<dynamic> response =md.GetManifestWithHttpInfo (urn) ;
httpErrorHandler (response, "Failed to get file manifest") ;
Console.WriteLine (response.Data.ToString () as string) ;
} catch ( Exception ex ) {
Console.Error.WriteLine ("Exception when calling DerivativesApi.GetManifestWithHttpInfo: " + ex.Message) ;
return (2) ;
}
return (0) ;
}
internal static int RunMetadata (MetadataOptions opts) {
string bucketKey =currentBucket (string.Empty) ;
if ( !checkBucketKey (bucketKey) )
return (2) ;
try {
if ( opts.file )
opts.fileKey =readFileKey (bucketKey, opts.fileKey) ;
Console.WriteLine ("Getting file metadata") ;
string urn =URN (bucketKey, opts.fileKey) ;
DerivativesApi md =new DerivativesApi () ;
md.Configuration.AccessToken =access_token () ;
ApiResponse<dynamic> response =md.GetMetadataWithHttpInfo (urn) ;
httpErrorHandler (response, "Failed to get file metadata") ;
Console.WriteLine (response.Data.ToString () as string) ;
} catch ( Exception ex ) {
Console.Error.WriteLine ("Exception when calling DerivativesApi.GetMetadataWithHttpInfo: " + ex.Message) ;
return (2) ;
}
return (0) ;
}
internal static int RunThumbnail (ThumbnailOptions opts) {
string bucketKey =currentBucket (string.Empty) ;
if ( !checkBucketKey (bucketKey) )
return (2) ;
try {
if ( opts.file )
opts.fileKey =readFileKey (bucketKey, opts.fileKey) ;
Console.WriteLine ("Getting file thumbnail") ;
string urn =URN (bucketKey, opts.fileKey) ;
DerivativesApi md =new DerivativesApi () ;
md.Configuration.AccessToken =access_token () ;
ApiResponse<dynamic> response =md.GetThumbnailWithHttpInfo (urn, 400, 400) ;
httpErrorHandler (response, "Failed to get file thumbnail") ;
Stream downloadObj =response.Data as Stream ;
downloadObj.Position =0 ;
//using ( BinaryReader sr =new BinaryReader (downloadObj) )
using ( FileStream outputFile =new FileStream (opts.outputFile, FileMode.Create) )
downloadObj.CopyTo (outputFile) ;
Console.WriteLine ("Thumbnail downloaded successfully") ;
} catch ( Exception ex ) {
Console.Error.WriteLine ("Exception when calling DerivativesApi.GetThumbnailWithHttpInfo: " + ex.Message) ;
return (2) ;
}
return (0) ;
}
internal static int RunDeleteFile (DeleteFileOptions opts) {
string bucketKey =currentBucket (string.Empty) ;
if ( !checkBucketKey (bucketKey) )
return (2) ;
try {
if ( opts.file )
opts.fileKey =readFileKey (bucketKey, opts.fileKey) ;
Console.WriteLine ("Deleting file: " + opts.fileKey) ;
ObjectsApi ossObjects =new ObjectsApi () ;
ossObjects.Configuration.AccessToken =access_token () ;
ApiResponse<dynamic> response =ossObjects.DeleteObjectWithHttpInfo (bucketKey, opts.fileKey) ;
httpErrorHandler (response, "Failed to delete file") ;
Console.WriteLine ("File deleted") ;
} catch ( Exception ex ) {
Console.Error.WriteLine ("Exception when calling ObjectsApi.DeleteObjectWithHttpInfo: " + ex.Message) ;
return (2) ;
}
return (0) ;
}
internal static int RunDeleteManifest (DeleteManifestOptions opts) {
string bucketKey =currentBucket (string.Empty) ;
if ( !checkBucketKey (bucketKey) )
return (2) ;
try {
if ( opts.file )
opts.fileKey =readFileKey (bucketKey, opts.fileKey) ;
Console.WriteLine ("Deleting manifest for " + opts.fileKey) ;
string urn =URN (bucketKey, opts.fileKey) ;
DerivativesApi md =new DerivativesApi () ;
md.Configuration.AccessToken =access_token () ;
ApiResponse<dynamic> response =md.DeleteManifestWithHttpInfo (urn) ;
httpErrorHandler (response, "Failed to delete manifest") ;
Console.WriteLine ("Manifest deleted") ;
} catch ( Exception ex ) {
Console.Error.WriteLine ("Exception when calling DerivativesApi.DeleteManifestWithHttpInfo: " + ex.Message) ;
return (2) ;
}
return (0) ;
}
internal static int RunHtml (HtmlOptions opts) {
string bucketKey =currentBucket (string.Empty) ;
if ( !checkBucketKey (bucketKey) )
return (2) ;
try {
if ( opts.file )
opts.fileKey =readFileKey (bucketKey, opts.fileKey) ;
Console.WriteLine ("Creating file: " + opts.outputFile) ;
string urn =URN (bucketKey, opts.fileKey) ;
string accessToken =readOnlyAccessToken () ;
string st =_html.Replace ("__URN__", urn).Replace ("__ACCESS_TOKEN__", accessToken) ;
System.IO.File.WriteAllText (opts.outputFile, st) ;
Console.WriteLine ("File created") ;
} catch ( Exception ex ) {
Console.Error.WriteLine ("Exception when calling DerivativesApi.DeleteManifestWithHttpInfo: " + ex.Message) ;
return (2) ;
}
return (0) ;
}
#endregion
#region Utils
internal static bool httpErrorHandler (ApiResponse<dynamic> response, string msg ="", bool bExit =true) {
if ( response.StatusCode < 200 || response.StatusCode >= 300 ) {
Console.Error.WriteLine (msg) ;
Console.Error.WriteLine ("HTTP " + response.StatusCode) ;
if ( bExit )
System.Environment.Exit (1) ;
return (true) ;
}
return (false) ;
}
public static bool hasOwnProperty (dynamic obj, string name) {
try {
var test =obj [name] ;
return (true) ;
} catch ( Exception ) {
return (false) ;
}
}
private static bool checkBucketKey (string name) {
Regex rgx =new Regex (@"^[-_.a-z0-9]{3,128}$") ;
bool result =rgx.IsMatch (name) ;
if ( !result )
Console.Error.WriteLine ("Invalid bucket name") ;
return (result) ;
}
internal static string currentBucket (string name) {
if ( string.IsNullOrEmpty (name) )
return (System.IO.File.ReadAllText ("data/bucket")) ;
return (name) ;
}
internal static string readFileKey (string bucketKey, string fileKey) {
try {
string details =System.IO.File.ReadAllText ("data/" + bucketKey + "." + fileKey + ".json") ;
JObject json =JObject.Parse (details) ;
return (json ["objectKey"].ToString ()) ;
} catch ( Exception ) {
return ("") ;
}
}
internal static string URN (string bucketKey, string fileKey, bool bSafe =true) {
string urn ="urn:adsk.objects:os.object:" + bucketKey + "/" + fileKey ;
try {
string details =System.IO.File.ReadAllText ("data/" + bucketKey + "." + fileKey + ".json") ;
JObject json =JObject.Parse (details) ;
urn =bSafe ? SafeBase64Encode (json ["objectId"].ToString ()) : Base64Encode (json ["objectId"].ToString ()) ;
} catch ( Exception ) {
}
return (urn) ;
}
private static Random random =new Random() ;
public static string RandomString (int length =8) {
const string chars ="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" ;
return (new string (Enumerable.Repeat (chars, length)
.Select (s => s [random.Next (s.Length)]).ToArray ())
) ;
}
public static string Base64Encode (string plainText) {
var plainTextBytes =System.Text.Encoding.UTF8.GetBytes (plainText) ;
return (System.Convert.ToBase64String (plainTextBytes)) ;
}
public static string Base64Decode (string base64EncodedData) {
var base64EncodedBytes =System.Convert.FromBase64String (base64EncodedData) ;
return (System.Text.Encoding.UTF8.GetString (base64EncodedBytes)) ;
}
private static readonly char [] padding ={ '=' } ;
public static string SafeBase64Encode (string plainText) {
var plainTextBytes =System.Text.Encoding.UTF8.GetBytes (plainText) ;
return (System.Convert.ToBase64String (plainTextBytes)
.TrimEnd (padding).Replace ('+', '-').Replace ('/', '_')
) ;
}
public static string SafeBase64Decode (string base64EncodedData) {
string st =base64EncodedData.Replace ('_', '/').Replace ('-', '+') ;
switch ( base64EncodedData.Length % 4 ) {
case 2: st +="==" ; break ;
case 3: st +="=" ; break ;
}
var base64EncodedBytes =System.Convert.FromBase64String (st) ;
return (System.Text.Encoding.UTF8.GetString (base64EncodedBytes)) ;
}
#endregion
#region Html
private static readonly string _html =@"<!DOCTYPE html>
<html>
<head>
<meta charset=""UTF-8"">
<script src=""https://developer.api.autodesk.com/viewingservice/v1/viewers/three.min.css""></script>
<link rel=""stylesheet"" href=""https://developer.api.autodesk.com/viewingservice/v1/viewers/style.min.css"" />
<script src=""https://developer.api.autodesk.com/viewingservice/v1/viewers/viewer3D.min.js""></script>
</head>
<body onload=""initialize()"">
<div id=""viewer"" style=""position:absolute; width:90%; height:90%;""></div>
<script>
function authMe () { return ('__ACCESS_TOKEN__') ; }