|
| 1 | +using CommandLine; |
| 2 | +using Newtonsoft.Json; |
| 3 | +using Quickshare; |
| 4 | +using System.Reflection; |
| 5 | +using uplink.NET.Interfaces; |
| 6 | +using uplink.NET.Models; |
| 7 | +using uplink.NET.Services; |
| 8 | + |
| 9 | +var assembly = Assembly.GetExecutingAssembly(); |
| 10 | +var assemblyVersion = assembly.GetName().Version; |
| 11 | +Console.ForegroundColor = ConsoleColor.Blue; |
| 12 | +Console.WriteLine($"\nWelcome to Quickshare v{assemblyVersion.Major}.{assemblyVersion.Minor}.{assemblyVersion.Build} powered by Storj DCS (https://storj.io)!\n"); |
| 13 | +Console.ForegroundColor = ConsoleColor.Gray; |
| 14 | + |
| 15 | +string configLocation = Environment.GetFolderPath(System.Environment.SpecialFolder.Personal) + @"\quickshare.config"; |
| 16 | +if (!File.Exists(configLocation)) |
| 17 | +{ |
| 18 | + Parser.Default.ParseArguments<CommandLineConfigOptions>(args) |
| 19 | + .WithParsed(o => |
| 20 | + { |
| 21 | + var quickshareConfig = new QuickshareConfig { AccessGrant = o.AccessGrant, BucketName = o.BucketName }; |
| 22 | + var configJson = JsonConvert.SerializeObject(quickshareConfig); |
| 23 | + File.WriteAllText(configLocation, configJson); |
| 24 | + |
| 25 | + Console.WriteLine("Your configuration has been set! You may now use 'quickshare FILENAME' to share a file."); |
| 26 | + }) |
| 27 | + .WithNotParsed(o => |
| 28 | + { |
| 29 | + Console.WriteLine("You need to setup Quickshare first. Call 'quickshare -g ACCESS_GRANT -b BUCKET_NAME'."); |
| 30 | + }); |
| 31 | +} |
| 32 | +else |
| 33 | +{ |
| 34 | + var quickshareConfig = JsonConvert.DeserializeObject<QuickshareConfig>(File.ReadAllText(configLocation)); |
| 35 | + if (quickshareConfig == null) |
| 36 | + { |
| 37 | + Console.WriteLine("Invalid config. The config has been removed - please re-init quickshare using 'quickshare config'."); |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + var result = Parser.Default.ParseArguments<CommandLineConfigOptions, CommandLineShareOptions>(args); |
| 42 | + result.WithParsed<CommandLineShareOptions>(s => |
| 43 | + { |
| 44 | + Console.WriteLine("Sharing file '" + s.Filename + "'."); |
| 45 | + |
| 46 | + Access access = new Access(quickshareConfig.AccessGrant); |
| 47 | + IBucketService bucketService = new BucketService(access); |
| 48 | + Console.WriteLine("Checking bucket..."); |
| 49 | + var bucket = bucketService.EnsureBucketAsync(quickshareConfig.BucketName).Result; |
| 50 | + Console.WriteLine("Starting upload..."); |
| 51 | + IObjectService objectService = new ObjectService(access); |
| 52 | + var uploadOperation = objectService.UploadObjectAsync(bucket, s.Filename, new UploadOptions(), File.ReadAllBytes(s.Filename), false).Result; |
| 53 | + uploadOperation.UploadOperationProgressChanged += (uploadOperation) => |
| 54 | + { |
| 55 | + if (uploadOperation.PercentageCompleted < 100) |
| 56 | + { |
| 57 | + Console.ForegroundColor = ConsoleColor.Yellow; |
| 58 | + Console.Write("\rStatus: {0}% ", uploadOperation.PercentageCompleted); |
| 59 | + } |
| 60 | + else |
| 61 | + { |
| 62 | + Console.ForegroundColor = ConsoleColor.Green; |
| 63 | + Console.Write("\rDone! \n"); |
| 64 | + Console.ForegroundColor = ConsoleColor.Gray; |
| 65 | + } |
| 66 | + }; |
| 67 | + uploadOperation.StartUploadAsync().Wait(); |
| 68 | + |
| 69 | + if (!uploadOperation.Completed) |
| 70 | + { |
| 71 | + Console.ForegroundColor = ConsoleColor.Red; |
| 72 | + Console.WriteLine("Upload failed: " + uploadOperation.ErrorMessage); |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + Console.WriteLine("Preparing file for sharing..."); |
| 77 | + var url = access.CreateShareURL(quickshareConfig.BucketName, s.Filename, true, true).Replace("gateway", "link"); |
| 78 | + Console.WriteLine("Your URL is:"); |
| 79 | + Console.WriteLine(url); |
| 80 | + TextCopy.ClipboardService.SetText(url); |
| 81 | + Console.WriteLine("It has been copied to the clipboard."); |
| 82 | + }); |
| 83 | +} |
| 84 | + |
| 85 | + |
0 commit comments