Esempio n. 1
0
        /// <summary>
        /// [异步async]上传文件
        /// </summary>
        /// <param name="localFile">本地待上传的文件名</param>
        /// <param name="saveKey">要保存的文件名称</param>
        /// <param name="token">上传凭证</param>
        /// <returns>上传文件后的返回结果</returns>
        public async Task <HttpResult> UploadFileAsync(StorageFile localFile, string saveKey, string token)
        {
            HttpResult result = new HttpResult();

            var fi = await localFile.GetBasicPropertiesAsync();

            if (fi.Size > (ulong)PUT_THRESHOLD)
            {
                if (recordFile == null)
                {
                    string recordKey = ResumeHelper.GetDefaultRecordKey(localFile.Path, saveKey);
                    recordFile = await(await UserEnv.GetHomeFolderAsync()).CreateFileAsync(recordKey, CreationCollisionOption.OpenIfExists);
                }
                if (upph == null)
                {
                    upph = new UploadProgressHandler(ResumableUploader.DefaultUploadProgressHandler);
                }

                if (upctl == null)
                {
                    upctl = new UploadController(ResumableUploader.DefaultUploadController);
                }

                ResumableUploader ru = new ResumableUploader(UPLOAD_FROM_CDN, CHUNK_UNIT);
                result = await ru.UploadFileAsync(localFile, saveKey, token, recordFile, upph, upctl);
            }
            else
            {
                FormUploader fu = new FormUploader(UPLOAD_FROM_CDN);
                result = await fu.UploadFileAsync(localFile, saveKey, token);
            }

            return(result);
        }
Esempio n. 2
0
        private async void Button_UploadData_Click(object sender, RoutedEventArgs e)
        {
            string saveKey = "uwp-upload-data-test-1";

            Mac       mac       = new Mac(AK, SK);
            PutPolicy putPolicy = new PutPolicy();

            putPolicy.Scope = "test";
            putPolicy.SetExpires(30);

            string jstr  = putPolicy.ToJsonString();
            string token = Auth.CreateUploadToken(mac, jstr);

            UploadProgressHandler upph = new UploadProgressHandler(MyUploadProgresHandler);

            UploadManager um = new UploadManager();

            um.SetUploadProgressHandler(upph);

            byte[] data = await FormUploader.ReadToByteArrayAsync(localFile);

            var result = await um.UploadDataAsync(data, saveKey, token);

            TextBox_Info.Text = result.ToString();
        }
Esempio n. 3
0
        /// <summary>
        /// [异步async]上传文件
        /// </summary>
        /// <param name="localFile">本地待上传的文件名</param>
        /// <param name="saveKey">要保存的文件名称</param>
        /// <param name="token">上传凭证</param>
        /// <returns>上传文件后的返回结果</returns>
        public async Task <HttpResult> UploadFileAsync(string localFile, string saveKey, string token)
        {
            HttpResult result = new HttpResult();

            var fi = new FileInfo(localFile);

            if (fi.Length > PUT_THRESHOLD)
            {
                if (recordFile == null)
                {
                    string recordKey = ResumeHelper.GetDefaultRecordKey(localFile, saveKey);
                    recordFile = Path.Combine(UserEnv.GetHomeFolder(), recordKey);
                }
                if (upph == null)
                {
                    upph = new UploadProgressHandler(ResumableUploader.DefaultUploadProgressHandler);
                }

                if (upctl == null)
                {
                    upctl = new UploadController(ResumableUploader.DefaultUploadController);
                }

                ResumableUploader ru = new ResumableUploader(UPLOAD_FROM_CDN, CHUNK_UNIT);
                result = await ru.UploadFileAsync(localFile, saveKey, token, recordFile, upph, upctl);
            }
            else
            {
                FormUploader fu = new FormUploader(UPLOAD_FROM_CDN);
                result = await fu.UploadFileAsync(localFile, saveKey, token);
            }

            return(result);
        }
Esempio n. 4
0
        /// <summary>
        /// 上传大文件,支持断点续上传
        /// </summary>
        public static void uploadBigFile()
        {
            Mac mac = new Mac(Settings.AccessKey, Settings.SecretKey);

            string bucket    = "test";
            string saveKey   = "video-x1-1.mp4";
            string localFile = "D:/QFL/1.mp4";

            // 断点记录文件,可以不用设置,让SDK自动生成,如果出现续上传的情况,SDK会尝试从该文件载入断点记录
            // SDK自动生成的文件为Path.Combine(UserEnv.GetHomeFolder(), ResumeHelper.GetDefaultRecordKey(localFile, saveKey))
            // 对于不同的上传任务,请使用不同的recordFile
            string recordFile = "D:/QFL/resume";

            PutPolicy putPolicy = new PutPolicy();

            putPolicy.Scope = bucket;
            putPolicy.SetExpires(3600);
            putPolicy.DeleteAfterDays = 1;

            // 将PutPolicy转换为JSON字符串
            string jstr = putPolicy.ToJsonString();

            string token = Auth.CreateUploadToken(mac, jstr);

            // 包含两个参数,并且都有默认值
            // 参数1(bool): uploadFromCDN是否从CDN加速上传,默认否
            // 参数2(enum): chunkUnit上传分片大小,可选值128KB,256KB,512KB,1024KB,2048KB,4096KB
            ResumableUploader ru = new ResumableUploader(false, ChunkUnit.U1024K);

            // ResumableUploader.uploadFile有多种形式,您可以根据需要来选择
            //
            // 最简模式,使用默认recordFile和默认uploadProgressHandler
            // UploadFile(localFile,saveKey,token)
            //
            // 基本模式,使用默认uploadProgressHandler
            // UploadFile(localFile,saveKey,token,recordFile)
            //
            // 一般模式,使用自定义进度处理(监视上传进度)
            // UploadFile(localFile,saveKey,token,recordFile,uploadProgressHandler)
            //
            // 高级模式,包含上传控制(可控制暂停/继续 或者强制终止)
            // UploadFile(localFile,saveKey,token,recordFile,uploadProgressHandler,uploadController)
            //
            // 高级模式,包含上传控制(可控制暂停 / 继续 或者强制终止), 可设置最大尝试次数
            // UploadFile(localFile,saveKey,token,recordFile,maxTry,uploadProgressHandler,uploadController)

            // 使用默认进度处理,使用自定义上传控制
            UploadProgressHandler upph  = new UploadProgressHandler(ResumableUploader.DefaultUploadProgressHandler);
            UploadController      upctl = new UploadController(uploadControl);

            var result = ru.UploadFile(localFile, saveKey, token, recordFile, upph, upctl);

            Console.WriteLine(result);
        }
Esempio n. 5
0
        internal void UploadToImageStore(
            string imageStoreConnectionString,
            string src,
            string des,
            UploadProgressHandler progressHandler = null)
        {
            if (string.IsNullOrEmpty(des))
            {
                des = this.GetPackageName(src);
            }

            if (!this.ValidateImageStorePath(des))
            {
                return;
            }

            if (progressHandler != null)
            {
                progressHandler.SetStateUploading();
            }

            IClusterConnection clusterConnection = null;

            if (imageStoreConnectionString.StartsWith(Constants.ImageStoreConnectionFabricType, StringComparison.OrdinalIgnoreCase))
            {
                clusterConnection = this.GetClusterConnection();

                if (clusterConnection.SecurityCredentials != null && clusterConnection.SecurityCredentials.CredentialType == CredentialType.Claims)
                {
                    clusterConnection.FabricClient.ImageStore.UploadContent(imageStoreConnectionString, des, src, progressHandler, this.GetTimeout(), CopyFlag.AtomicCopy, false);
                }
                else
                {
                    clusterConnection = null;
                }
            }

            if (clusterConnection == null)
            {
                var imageStore = this.CreateImageStore(imageStoreConnectionString, Path.GetTempPath());
                imageStore.UploadContent(
                    des,
                    src,
                    progressHandler,
                    this.GetTimeout(),
                    CopyFlag.AtomicCopy,
                    false);
            }

            this.WriteObject(StringResources.Info_UploadToImageStoreSucceeded);
        }
        protected override void ProcessRecord()
        {
            try
            {
                if (!this.TimeoutSec.HasValue)
                {
                    this.TimeoutSec = DefaultTimeoutInSec;
                }

                var imageStoreString = (this.SkipCopy && !this.GenerateChecksums)
                    ? string.Empty
                    : this.TryFetchImageStoreConnectionString(this.ImageStoreConnectionString, this.CertStoreLocation);

                var sourcePath = this.GetAbsolutePath(this.ApplicationPackagePath);

                using (var handler = new UploadProgressHandler(sourcePath, this.ShowProgressIntervalMilliseconds))
                {
                    var progressHandler = this.ShowProgress ? handler : null;

                    if (progressHandler != null)
                    {
                        progressHandler.Initialize();
                    }

                    sourcePath = this.CopyPackageIfNeeded(sourcePath, progressHandler);

                    this.CompressOrUncompressIfNeeded(sourcePath, progressHandler);

                    this.GenerateChecksumsIfNeeded(sourcePath, imageStoreString, progressHandler);

                    if (this.SkipCopy)
                    {
                        return;
                    }

                    this.UploadToImageStore(
                        imageStoreString,
                        sourcePath,
                        this.ApplicationPackagePathInImageStore,
                        progressHandler);
                }
            }
            catch (Exception exception)
            {
                this.ThrowTerminatingError(
                    exception,
                    Constants.CopyApplicationPackageErrorId,
                    null);
            }
        }
Esempio n. 7
0
        /// <summary>
        /// 上传大文件,可以从上次的断点位置继续上传
        /// </summary>
        public void UploadBigFile()
        {
            var token = QiNiuHelper.GetToken();


            string saveKey   = "1.mp4";
            string localFile = "D:\\QFL\\1.mp4";
            // 断点记录文件,可以不用设置,让SDK自动生成,如果出现续上传的情况,SDK会尝试从该文件载入断点记录
            // 对于不同的上传任务,请使用不同的recordFile
            string recordFile = "D:\\QFL\\resume.12345";

            // 包含两个参数,并且都有默认值
            // 参数1(bool): uploadFromCDN是否从CDN加速上传,默认否
            // 参数2(enum): chunkUnit上传分片大小,可选值128KB,256KB,512KB,1024KB,2048KB,4096KB
            ResumableUploader ru = new ResumableUploader(false, ChunkUnit.U1024K);
            // ResumableUploader.UploadFile有多种形式,您可以根据需要来选择
            //
            // 最简模式,使用默认recordFile和默认uploadProgressHandler
            //UploadFile(localFile,saveKey,token)
            //
            // 基本模式,使用默认uploadProgressHandler
            // UploadFile(localFile,saveKey,token,recordFile)
            //
            // 一般模式,使用自定义进度处理(可以监视上传进度)
            // UploadFile(localFile,saveKey,token,recordFile,uploadProgressHandler)
            //
            // 高级模式,包含上传控制(可控制暂停/继续 或者强制终止)
            // UploadFile(localFile,saveKey,token,recordFile,uploadProgressHandler,uploadController)
            //
            // 支持自定义参数
            //var extra = new System.Collections.Generic.Dictionary<string, string>();
            //extra.Add("FileType", "UploadFromLocal");
            //extra.Add("YourKey", "YourValue");
            //uploadFile(...,extra,...)
            //最大尝试次数(有效值1~20),在上传过程中(如mkblk或者bput操作)如果发生错误,它将自动重试,如果没有错误则无需重试
            int maxTry = 10;
            // 使用默认进度处理,使用自定义上传控制
            UploadProgressHandler upph   = new UploadProgressHandler(ResumableUploader.DefaultUploadProgressHandler);
            UploadController      upctl  = new UploadController(uploadControl);
            HttpResult            result = ru.UploadFile(localFile, saveKey, token, recordFile, maxTry, upph, upctl);

            Console.WriteLine(result);
        }
        protected override async Task UploadFileAsync(FileInfo fileInfo, int?expireIn = null, CancellationToken cancellationToken = default)
        {
            Mac mac = new Mac(Options.AccessKey, Options.SecretKey);

            PutPolicy putPolicy = new PutPolicy
            {
                Scope            = fileInfo.Directory + ":" + fileInfo.Name,
                CallbackBody     = Options.UploadCallbackBody,
                CallbackBodyType = Options.UploadCallbackBodyType,
                CallbackHost     = Options.UploadCallbackHost,
                CallbackUrl      = Options.UploadCallbackUrl
            };

            if (expireIn.HasValue)
            {
                putPolicy.SetExpires(expireIn.Value);
            }
            if (Options.DeleteAfterDays > 0)
            {
                putPolicy.DeleteAfterDays = Options.DeleteAfterDays;
            }


            string jstr  = putPolicy.ToJsonString();
            string token = Auth.CreateUploadToken(mac, jstr);

            UploadProgressHandler handler = (uploadByte, totalByte) =>
            {
                OnFileUploadProgressChanged(uploadByte, totalByte);
            };

            // 带进度的上传
            ResumableUploader uploader = new ResumableUploader();
            var httpResult             = await uploader.UploadDataAsync(fileInfo.Data, fileInfo.Name, token, handler);

            // 普通上传
            //FormUploader fu = new FormUploader();
            //var httpResult = await fu.UploadDataAsync(fileInfo.Data, fileInfo.Name, token);

            // TODO: 处理响应代码

            Console.WriteLine(httpResult.Code);
        }
        private void CompressOrUncompressIfNeeded(string sourcePath, UploadProgressHandler progressHandler)
        {
            if (this.CompressPackage)
            {
                if (progressHandler != null)
                {
                    progressHandler.SetStateCompressing();
                }

                ImageStoreUtility.ArchiveApplicationPackage(sourcePath, progressHandler);
            }
            else if (this.UncompressPackage)
            {
                if (progressHandler != null)
                {
                    progressHandler.SetStateUncompressing();
                }

                ImageStoreUtility.TryExtractApplicationPackage(sourcePath, progressHandler);
            }
        }
        private string CopyPackageIfNeeded(string sourcePath, UploadProgressHandler progressHandler)
        {
            if (!string.IsNullOrEmpty(this.ApplicationPackageCopyPath))
            {
                var destPath = this.GetAbsolutePath(this.ApplicationPackageCopyPath);

                if (progressHandler != null)
                {
                    progressHandler.SetStateCopying(destPath);
                }

                FabricDirectory.Copy(sourcePath, destPath, true);

                if (progressHandler != null)
                {
                    progressHandler.UpdateApplicationPackagePath(destPath);
                }

                sourcePath = destPath;
            }

            return(sourcePath);
        }
Esempio n. 11
0
        public void uploadFile(object obj)
        {
            FileItem item = obj as FileItem;

            if (syncProgressPage.checkCancelSignal() && item.Length > syncSetting.ChunkUploadThreshold)
            {
                this.doneEvent.Set();
                return;
            }

            string fileFullPath = item.LocalFile;

            if (!File.Exists(fileFullPath))
            {
                Log.Error(string.Format("file not found error, {0}", fileFullPath));
                this.doneEvent.Set();
                return;
            }

            //set upload params
            int  putThreshold  = this.syncSetting.ChunkUploadThreshold;
            int  chunkSize     = this.syncSetting.DefaultChunkSize;
            bool uploadFromCDN = this.syncSetting.UploadFromCDN;

            string myDocPath  = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            string recordPath = System.IO.Path.Combine(myDocPath, "qsunsync", "resume");

            if (!Directory.Exists(recordPath))
            {
                Directory.CreateDirectory(recordPath);
            }

            Mac mac = new Mac(SystemConfig.ACCESS_KEY, SystemConfig.SECRET_KEY);

            //current file info
            FileInfo fileInfo         = new FileInfo(fileFullPath);
            long     fileLength       = fileInfo.Length;
            string   fileLastModified = fileInfo.LastWriteTimeUtc.ToFileTime().ToString();
            //support resume upload
            string recorderKey = string.Format("{0}:{1}:{2}:{3}:{4}", this.syncSetting.LocalDirectory,
                                               this.syncSetting.TargetBucket, item.SaveKey, fileFullPath, fileLastModified);

            recorderKey = Hashing.CalcMD5X(recorderKey);

            this.syncProgressPage.updateUploadLog("准备上传文件 " + fileFullPath);

            PutPolicy putPolicy = new PutPolicy();

            if (this.syncSetting.OverwriteDuplicate)
            {
                putPolicy.Scope = this.syncSetting.TargetBucket + ":" + item.SaveKey;
            }
            else
            {
                putPolicy.Scope = this.syncSetting.TargetBucket;
            }
            putPolicy.SetExpires(24 * 30 * 3600);

            string uptoken = Auth.CreateUploadToken(mac, putPolicy.ToJsonString());

            this.syncProgressPage.updateUploadLog("开始上传文件 " + fileFullPath);

            HttpResult result = null;

            ChunkUnit cu = (ChunkUnit)(chunkSize / (128 * 1024));

            if (item.Length > putThreshold)
            {
                ResumableUploader ru         = new ResumableUploader(uploadFromCDN, cu);
                string            recordFile = System.IO.Path.Combine(recordPath, Hashing.CalcMD5X(fileFullPath));

                UploadProgressHandler upph = new UploadProgressHandler(delegate(long uploaded, long total)
                {
                    this.syncProgressPage.updateSingleFileProgress(taskId, fileFullPath, item.SaveKey, uploaded, fileLength);
                });

                result = ru.UploadFile(fileFullPath, item.SaveKey, uptoken, recordFile, upph, upController);
            }
            else
            {
                FormUploader su = new FormUploader(uploadFromCDN);
                result = su.UploadFile(fileFullPath, item.SaveKey, uptoken);
            }

            if (result.Code == (int)HttpCode.OK)
            {
                item.Uploaded = true;
                this.syncProgressPage.updateUploadLog("上传成功 " + fileFullPath);
                this.syncProgressPage.addFileUploadSuccessLog(string.Format("{0}\t{1}\t{2}", this.syncSetting.TargetBucket,
                                                                            fileFullPath, item.SaveKey));
                this.syncProgressPage.updateTotalUploadProgress();
            }
            else
            {
                item.Uploaded = false;
                this.syncProgressPage.updateUploadLog("上传失败 " + fileFullPath + "," + result.Text);
                this.syncProgressPage.addFileUploadErrorLog(string.Format("{0}\t{1}\t{2}\t{3}", this.syncSetting.TargetBucket,
                                                                          fileFullPath, item.SaveKey, result.Text));
            }

            this.doneEvent.Set();
        }
Esempio n. 12
0
        public void uploadFile(object file)
        {
            if (syncProgressPage.checkCancelSignal())
            {
                this.doneEvent.Set();
                return;
            }
            string fileFullPath = file.ToString();

            if (!File.Exists(fileFullPath))
            {
                Log.Error(string.Format("file not found error, {0}", fileFullPath));
                this.doneEvent.Set();
                return;
            }
            //check skipped rules
            int    fileRelativePathIndex = fileFullPath.IndexOf(this.syncSetting.SyncLocalDir);
            string fileRelativePath      = fileFullPath.Substring(fileRelativePathIndex + this.syncSetting.SyncLocalDir.Length);

            if (fileRelativePath.StartsWith("\\"))
            {
                fileRelativePath = fileRelativePath.Substring(1);
            }

            string[] skippedPrefixes = this.syncSetting.SkipPrefixes.Split(',');

            foreach (string prefix in skippedPrefixes)
            {
                if (!string.IsNullOrWhiteSpace(prefix))
                {
                    if (fileRelativePath.StartsWith(prefix.Trim()))
                    {
                        //skip by prefix
                        this.syncProgressPage.addFileSkippedLog(string.Format("{0}\t{1}", this.syncSetting.SyncTargetBucket,
                                                                              fileFullPath));
                        this.syncProgressPage.updateUploadLog("按照前缀规则跳过文件不同步 " + fileFullPath);
                        this.syncProgressPage.updateTotalUploadProgress();
                        this.doneEvent.Set();
                        return;
                    }
                }
            }

            string[] skippedSuffixes = this.syncSetting.SkipSuffixes.Split(',');
            foreach (string suffix in skippedSuffixes)
            {
                if (!string.IsNullOrWhiteSpace(suffix))
                {
                    if (fileRelativePath.EndsWith(suffix.Trim()))
                    {
                        //skip by suffix
                        this.syncProgressPage.addFileSkippedLog(string.Format("{0}\t{1}", this.syncSetting.SyncTargetBucket,
                                                                              fileFullPath));
                        this.syncProgressPage.updateUploadLog("按照后缀规则跳过文件不同步 " + fileFullPath);
                        this.syncProgressPage.updateTotalUploadProgress();
                        this.doneEvent.Set();
                        return;
                    }
                }
            }

            //generate the file key
            string fileKey = "";

            if (this.syncSetting.IgnoreDir)
            {
                //check ignore dir
                fileKey = System.IO.Path.GetFileName(fileFullPath);
            }
            else
            {
                string newFileFullPath = fileFullPath.Replace('\\', '/');
                string newLocalSyncDir = this.syncSetting.SyncLocalDir.Replace('\\', '/');
                int    fileKeyIndex    = newFileFullPath.IndexOf(newLocalSyncDir);
                fileKey = newFileFullPath.Substring(fileKeyIndex + newLocalSyncDir.Length);
                if (fileKey.StartsWith("/"))
                {
                    fileKey = fileKey.Substring(1);
                }
            }

            //add prefix
            fileKey = this.syncSetting.SyncPrefix + fileKey;

            //set upload params

            string myDocPath  = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            string recordPath = System.IO.Path.Combine(myDocPath, "qsunsync", "resume");

            if (!Directory.Exists(recordPath))
            {
                Directory.CreateDirectory(recordPath);
            }

            bool overwriteUpload = false;
            bool uploadByCdn     = true;

            switch (this.syncSetting.UploadEntryDomain)
            {
            case 1:
                uploadByCdn = false; break;

            default:
                uploadByCdn = true; break;
            }
            Mac mac = new Mac(SystemConfig.ACCESS_KEY, SystemConfig.SECRET_KEY);
            //set bucket manager and upload manager config
            Config config = new Config();

            config.UseCdnDomains = uploadByCdn;
            config.PutThreshold  = this.syncSetting.ChunkUploadThreshold;
            if (!string.IsNullOrEmpty(SystemConfig.UP_DOMAIN) && !string.IsNullOrEmpty(SystemConfig.RS_DOMAIN))
            {
                config.Zone = new Zone
                {
                    RsHost     = SystemConfig.RS_DOMAIN,
                    SrcUpHosts = new string[] { SystemConfig.UP_DOMAIN },
                    CdnUpHosts = new string[] { SystemConfig.UP_DOMAIN }
                };
            }

            //current file info
            System.IO.FileInfo fileInfo = new System.IO.FileInfo(fileFullPath);
            long   fileLength           = fileInfo.Length;
            string fileLastModified     = fileInfo.LastWriteTimeUtc.ToFileTime().ToString();
            //support resume upload
            string recorderKey = string.Format("{0}:{1}:{2}:{3}:{4}", this.syncSetting.SyncLocalDir,
                                               this.syncSetting.SyncTargetBucket, fileKey, fileFullPath, fileLastModified);

            recorderKey = Tools.md5Hash(recorderKey);

            if (syncSetting.CheckRemoteDuplicate)
            {
                //check remotely
                BucketManager bucketManager = new BucketManager(mac, config);
                StatResult    statResult    = bucketManager.Stat(this.syncSetting.SyncTargetBucket, fileKey);

                if (statResult.Result != null && !string.IsNullOrEmpty(statResult.Result.Hash))
                {
                    //file exists in bucket
                    string localHash = "";
                    //cached file info
                    try
                    {
                        CachedHash cachedHash = CachedHash.GetCachedHashByLocalPath(fileFullPath, localHashDB);
                        string     cachedEtag = cachedHash.Etag;
                        string     cachedLmd  = cachedHash.LastModified;
                        if (!string.IsNullOrEmpty(cachedEtag) && !string.IsNullOrEmpty(cachedLmd))
                        {
                            if (cachedLmd.Equals(fileLastModified))
                            {
                                //file not modified
                                localHash = cachedEtag;
                            }
                            else
                            {
                                //file modified, calc the hash and update db
                                string newEtag = Qiniu.Util.ETag.CalcHash(fileFullPath);
                                localHash = newEtag;
                                try
                                {
                                    CachedHash.UpdateCachedHash(fileFullPath, newEtag, fileLastModified, localHashDB);
                                }
                                catch (Exception ex)
                                {
                                    Log.Error(string.Format("update local hash failed {0}", ex.Message));
                                }
                            }
                        }
                        else
                        {
                            //no record, calc hash and insert into db
                            string newEtag = Qiniu.Util.ETag.CalcHash(fileFullPath);
                            localHash = newEtag;
                            try
                            {
                                CachedHash.InsertCachedHash(fileFullPath, newEtag, fileLastModified, localHashDB);
                            }
                            catch (Exception ex)
                            {
                                Log.Error(string.Format("insert local hash failed {0}", ex.Message));
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.Error(string.Format("get hash from local db failed {0}", ex.Message));
                        localHash = Qiniu.Util.ETag.CalcHash(fileFullPath);
                    }

                    if (localHash.Equals(statResult.Result.Hash))
                    {
                        //same file, no need to upload
                        this.syncProgressPage.addFileExistsLog(string.Format("{0}\t{1}\t{2}", this.syncSetting.SyncTargetBucket,
                                                                             fileFullPath, fileKey));
                        this.syncProgressPage.updateUploadLog("空间已存在,跳过文件 " + fileFullPath);
                        this.syncProgressPage.updateTotalUploadProgress();

                        //compatible, insert or update sync log for file
                        try
                        {
                            SyncLog.InsertOrUpdateSyncLog(fileKey, fileFullPath, fileLastModified, this.syncLogDB);
                        }
                        catch (Exception ex)
                        {
                            Log.Error(string.Format("insert ot update sync log error {0}", ex.Message));
                        }

                        this.doneEvent.Set();
                        return;
                    }
                    else
                    {
                        if (this.syncSetting.OverwriteFile)
                        {
                            overwriteUpload = true;
                            this.syncProgressPage.updateUploadLog("空间已存在,将覆盖 " + fileFullPath);
                        }
                        else
                        {
                            this.syncProgressPage.updateUploadLog("空间已存在,不覆盖 " + fileFullPath);
                            this.syncProgressPage.addFileNotOverwriteLog(string.Format("{0}\t{1}\t{2}", this.syncSetting.SyncTargetBucket,
                                                                                       fileFullPath, fileKey));
                            this.syncProgressPage.updateTotalUploadProgress();
                            this.doneEvent.Set();
                            return;
                        }
                    }
                }
            }
            else
            {
                //check locally
                try
                {
                    SyncLog syncLog = SyncLog.GetSyncLogByKey(fileKey, this.syncLogDB);
                    if (!string.IsNullOrEmpty(syncLog.Key))
                    {
                        //has sync log and check whether it changes
                        if (syncLog.LocalPath.Equals(fileFullPath) && syncLog.LastModified.Equals(fileLastModified))
                        {
                            this.syncProgressPage.addFileExistsLog(string.Format("{0}\t{1}\t{2}", this.syncSetting.SyncTargetBucket,
                                                                                 fileFullPath, fileKey));
                            this.syncProgressPage.updateUploadLog("本地检查已同步,跳过" + fileFullPath);
                            this.syncProgressPage.updateTotalUploadProgress();
                            this.doneEvent.Set();
                            return;
                        }
                    }
                }
                catch (Exception ex)
                {
                    Log.Error(string.Format("get sync log failed {0}", ex.Message));
                    this.doneEvent.Set();
                    return;
                }

                if (this.syncSetting.OverwriteFile)
                {
                    overwriteUpload = true;
                }
            }

            //if file not exists or need to overwrite
            this.syncProgressPage.updateUploadLog("准备上传文件 " + fileFullPath);
            string resumeFile = System.IO.Path.Combine(recordPath, recorderKey);

            UploadManager uploadManager = new UploadManager(config);

            PutExtra putExtra = new PutExtra();

            putExtra.ResumeRecordFile   = resumeFile;
            putExtra.BlockUploadThreads = this.syncSetting.DefaultChunkSize;

            UploadProgressHandler progressHandler = new UploadProgressHandler(delegate(long uploadBytes, long totalBytes)
            {
                Console.WriteLine("progress: " + uploadBytes + ", " + totalBytes);
                double percent = uploadBytes * 1.0 / totalBytes;
                this.syncProgressPage.updateSingleFileProgress(taskId, fileFullPath, fileKey, fileLength, percent);
            });

            putExtra.ProgressHandler  = progressHandler;
            putExtra.UploadController = new UploadController(delegate
            {
                if (this.syncProgressPage.FinishSignal)
                {
                    return(UploadControllerAction.Aborted);
                }
                else
                {
                    if (this.syncProgressPage.CancelSignal)
                    {
                        return(UploadControllerAction.Suspended);
                    }
                    else
                    {
                        return(UploadControllerAction.Activated);
                    }
                }
            });

            PutPolicy putPolicy = new PutPolicy();

            if (overwriteUpload)
            {
                putPolicy.Scope = this.syncSetting.SyncTargetBucket + ":" + fileKey;
            }
            else
            {
                putPolicy.Scope = this.syncSetting.SyncTargetBucket;
            }
            putPolicy.SetExpires(24 * 30 * 3600);
            //set file type
            putPolicy.FileType = this.syncSetting.FileType;
            Auth   auth    = new Auth(mac);
            string uptoken = auth.CreateUploadToken(putPolicy.ToJsonString());

            this.syncProgressPage.updateUploadLog("开始上传文件 " + fileFullPath);

            HttpResult uploadResult = uploadManager.UploadFile(fileFullPath, fileKey, uptoken, putExtra);

            if (uploadResult.Code != 200)
            {
                this.syncProgressPage.updateUploadLog("上传失败 " + fileFullPath + "," + uploadResult.RefText);
                this.syncProgressPage.addFileUploadErrorLog(string.Format("{0}\t{1}\t{2}\t{3}", this.syncSetting.SyncTargetBucket,
                                                                          fileFullPath, fileKey, uploadResult.Text + "" + uploadResult.RefText));

                //file exists error
                if (uploadResult.Code == 614)
                {
                    this.syncProgressPage.updateUploadLog("空间已存在,未覆盖 " + fileFullPath);
                    this.syncProgressPage.addFileNotOverwriteLog(string.Format("{0}\t{1}\t{2}", this.syncSetting.SyncTargetBucket,
                                                                               fileFullPath, fileKey));
                }
            }
            else
            {
                //insert or update sync log for file
                try
                {
                    SyncLog.InsertOrUpdateSyncLog(fileKey, fileFullPath, fileLastModified, this.syncLogDB);
                }
                catch (Exception ex)
                {
                    Log.Error(string.Format("insert ot update sync log error {0}", ex.Message));
                }

                //write new file hash to local db
                if (!overwriteUpload)
                {
                    PutRet putRet   = JsonConvert.DeserializeObject <PutRet>(uploadResult.Text);
                    string fileHash = putRet.Hash;
                    if (this.localHashDB != null)
                    {
                        try
                        {
                            CachedHash.InsertOrUpdateCachedHash(fileFullPath, fileHash, fileLastModified, this.localHashDB);
                        }
                        catch (Exception ex)
                        {
                            Log.Error(string.Format("insert or update cached hash error {0}", ex.Message));
                        }
                    }
                    Log.Debug(string.Format("insert or update qiniu hash to local: '{0}' => '{1}'", fileFullPath, fileHash));
                }

                //update
                if (overwriteUpload)
                {
                    this.syncProgressPage.addFileOverwriteLog(string.Format("{0}\t{1}\t{2}", this.syncSetting.SyncTargetBucket,
                                                                            fileFullPath, fileKey));
                }
                this.syncProgressPage.updateUploadLog("上传成功 " + fileFullPath);
                this.syncProgressPage.addFileUploadSuccessLog(string.Format("{0}\t{1}\t{2}", this.syncSetting.SyncTargetBucket,
                                                                            fileFullPath, fileKey));
                this.syncProgressPage.updateTotalUploadProgress();
            }
            Log.Info("upload finish for " + fileFullPath + ", " + uploadResult.RefText);
            this.doneEvent.Set();
            return;
        }
Esempio n. 13
0
 /// <summary>
 /// 设置上传进度处理器-仅对于上传大文件有效,如果不设置则使用默认的进度处理器
 /// </summary>
 /// <param name="upph">上传进度处理器</param>
 public void SetUploadProgressHandler(UploadProgressHandler upph)
 {
     this.upph = upph;
 }
Esempio n. 14
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();
                }));
            });
        }
        private void GenerateChecksumsIfNeeded(string sourcePath, string imageStoreString, UploadProgressHandler progressHandler)
        {
            if (this.GenerateChecksums)
            {
                if (string.IsNullOrEmpty(imageStoreString))
                {
                    this.ThrowTerminatingError(
                        new ArgumentException(StringResources.Error_MissingImageStoreConnectionStringArgument),
                        Constants.CopyApplicationPackageErrorId,
                        null);
                }

                if (progressHandler != null)
                {
                    progressHandler.SetStateGeneratingChecksums();
                }

                ImageStoreUtility.GenerateApplicationPackageChecksumFiles(
                    sourcePath,
                    progressHandler,
                    ImageStoreUtility.GetImageStoreProviderType(imageStoreString) == ImageStoreProviderType.ImageStoreService);
            }
        }