private static async Task UploadQiniuAsync()
        {
            var accessKey = Environment.GetEnvironmentVariable("ak");
            var secretKey = Environment.GetEnvironmentVariable("sk");

            if (string.IsNullOrEmpty(accessKey) || string.IsNullOrEmpty(secretKey))
            {
                return;
            }
            var          mac       = new Mac(accessKey, secretKey);
            const string bucket    = "langpack";
            var          putPolicy = new PutPolicy
            {
                Scope = bucket
            };
            var key = "Minecraft-Mod-Language-Modpack.zip";

            putPolicy.SetExpires(120);
            var token         = Auth.CreateUploadToken(mac, putPolicy.ToJsonString());
            var uploadManager = new UploadManager(new Config());
            var result        = await uploadManager.UploadFile(@"./Minecraft-Mod-Language-Modpack.zip",
                                                               "Minecraft-Mod-Language-Modpack.zip", token, new PutExtra());

            Trace.Assert(result.RefInfo["key"] == key);
            Trace.Assert(result.RefInfo["hash"] == ETag.CalcHash(@"./Minecraft-Mod-Language-Modpack.zip"));
            Console.WriteLine(result.Text);
            var cdnManager    = new CdnManager(mac);
            var refreshResult = await cdnManager.RefreshUrls(new[]
                                                             { "http://downloader.meitangdehulu.com/Minecraft-Mod-Language-Modpack.zip" });

            Console.WriteLine(refreshResult.Result);
        }
示例#2
0
        private void CalcQETagButton_Click(object sender, RoutedEventArgs e)
        {
            string fileName = this.QETagSourceFileTextBox.Text;

            if (File.Exists(fileName))
            {
                try
                {
                    string qetag = ETag.CalcHash(fileName);
                    this.QETagResultTextBox.Text = qetag;
                }
                catch (Exception) { }
            }
        }
示例#3
0
        private void UploadButton_Click(object sender, RoutedEventArgs e)
        {
            string uploadToken = this.UploadTokenTextBox.Text.Trim();
            string filePath    = this.UploadFileTextBox.Text.Trim();
            string key         = this.UploadKeyTextBox.Text;

            if (string.IsNullOrEmpty(uploadToken) || string.IsNullOrEmpty(filePath))
            {
                return;
            }

            if (!this.EnableKeyCheckBox.IsChecked.Value)
            {
                key = null;
            }

            if (!File.Exists(filePath))
            {
                return;
            }

            // 移动代码(UploadButton_Click-->CreateTokenButton_Click) @fengyh, 2016-08-17-11:29
            //string mimeType = this.MimeTypeTextBox.Text.Trim();

            bool checkCrc32 = false;

            checkCrc32 = this.CheckCrc32CheckBox.IsChecked.Value;
            // 移动代码(UploadButton_Click-->CreateTokenButton_Click) @fengyh, 2016-08-17-11:29
            //if (mimeType.Length == 0)
            //{
            //    //mimeType = null;
            //    mimeType = "application/octet-stream";
            //}

            string extraParamKey1   = this.ExtraParamKeyTextBox1.Text.Trim();
            string extraParamValue1 = this.ExtraParamValueTextBox1.Text.Trim();
            string extraParamKey2   = this.ExtraParamKeyTextBox2.Text.Trim();
            string extraParamValue2 = this.ExtraParamValueTextBox2.Text.Trim();
            string extraParamKey3   = this.ExtraParamKeyTextBox3.Text.Trim();
            string extraParamValue3 = this.ExtraParamValueTextBox3.Text.Trim();

            string recordKey = null;

            //update status
            this.cancelUpload = false;
            this.UploadProgressBar.Visibility = Visibility.Visible;

            //start upload
            Task.Factory.StartNew(() =>
            {
                string qetag = ETag.CalcHash(filePath);
                if (key == null)
                {
                    recordKey = Base64.UrlSafeBase64Encode(qetag);
                }
                else
                {
                    recordKey = Base64.UrlSafeBase64Encode(key + qetag);
                }

                Dictionary <string, string> extraParams = new Dictionary <string, string>();
                if (!extraParams.ContainsKey(extraParamKey1))
                {
                    extraParams.Add(extraParamKey1, extraParamValue1);
                }
                if (!extraParams.ContainsKey(extraParamKey2))
                {
                    extraParams.Add(extraParamKey2, extraParamValue2);
                }
                if (!extraParams.ContainsKey(extraParamKey3))
                {
                    extraParams.Add(extraParamKey3, extraParamValue3);
                }

                System.IO.FileInfo fi = new FileInfo(filePath);

                Qiniu.Http.HttpResult result = new Qiniu.Http.HttpResult();

                if (fi.Length > 4 * 1024 * 1024)
                {
                    UploadProgressHandler upph = delegate(long u, long t)
                    {
                        Dispatcher.BeginInvoke((Action)(() =>
                        {
                            this.UploadProgressBar.Value = (int)(100.0 * u / t);
                        }));
                    };

                    UploadController upc = new UploadController(UploadControl);

                    ResumableUploader uploader = new ResumableUploader();

                    result = uploader.UploadFile(filePath, key, uploadToken, recordKey, 1, upph, upc, extraParams);
                }

                else
                {
                    FormUploader fu = new FormUploader();
                    result          = fu.UploadFile(filePath, key, uploadToken, extraParams);
                }

                Dispatcher.BeginInvoke((Action)(() =>
                {
                    this.TextBox_UploadResultText.Text = result.Text;
                    this.TextBox_UploadResultString.Text = result.ToString();
                }));
            });
        }