/// <summary> /// 生成默认的断点记录文件名称 /// </summary> /// <param name="localFile">待上传的本地文件</param> /// <param name="key">要保存的目标key</param> /// <returns>用于记录断点信息的文件名</returns> public static string GetDefaultRecordKey(string localFile, string key) { string tempDir = IOUtils.Api.GetTempPath(); var fileInfo = IOUtils.Api.GetFileInfo(localFile); string uniqueKey = string.Format("{0}:{1}:{2}", localFile, key, fileInfo.UpdatedAt.ToFileTime()); return(string.Format("{0}\\{1}", tempDir, "QiniuResume_" + Hashing.CalcMD5X(uniqueKey))); }
/// <summary> /// 生成默认的断点记录文件名称 /// </summary> /// <param name="localFile">待上传的本地文件</param> /// <param name="key">要保存的目标key</param> /// <returns>用于记录断点信息的文件名</returns> public static string GetDefaultRecordKey(string localFile, string key) { string tempDir = System.Environment.GetEnvironmentVariable("TEMP"); System.IO.FileInfo fileInfo = new System.IO.FileInfo(localFile); string uniqueKey = string.Format("{0}:{1}:{2}", localFile, key, fileInfo.LastWriteTime.ToFileTime()); return(string.Format("{0}\\{1}", tempDir, "QiniuResume_" + Hashing.CalcMD5X(uniqueKey))); }
/// <summary> /// 时间戳防盗链 /// 另请参阅https://support.qiniu.com/question/195128 /// </summary> /// <param name="request">“时间戳防盗链”请求,详情请参阅该类型的说明</param> /// <returns>时间戳防盗链接</returns> public string CreateTimestampAntiLeechUrl(TimestampAntiLeechUrlRequest request) { string RAW = request.RawUrl; string key = request.Key; string path = Uri.EscapeUriString(request.Path); string file = request.File; string query = request.Query; string ts = (long.Parse(request.Timestamp)).ToString("x"); string SIGN = Hashing.CalcMD5X(key + path + file + ts); string LEAD = query + "&"; if (string.IsNullOrEmpty(query)) { LEAD = "?"; } return(string.Format("{0}{1}sign={2}&t={3}", RAW, LEAD, SIGN, ts)); }
/// <summary> /// 时间戳防盗链 /// </summary> /// <param name="host">主机,如http://domain.com</param> /// <param name="fileName">文件名,如 hello/world/test.jpg</param> /// <param name="query">请求参数,如?v=1.1</param> /// <param name="encryptKey">后台提供的key</param> /// <param name="expireInSeconds">链接有效时长</param> /// <returns>时间戳防盗链接</returns> public static string CreateTimestampAntiLeechUrl(string host, string fileName, string query, string encryptKey, int expireInSeconds) { long expireAt = UnixTimestamp.GetUnixTimestamp(expireInSeconds); string expireHex = expireAt.ToString("x"); string path = string.Format("/{0}", Uri.EscapeUriString(fileName)); string toSign = string.Format("{0}{1}{2}", encryptKey, path, expireHex); string sign = Hashing.CalcMD5X(toSign); string finalUrl = null; if (!string.IsNullOrEmpty(query)) { finalUrl = string.Format("{0}{1}?{2}&sign={3}&t={4}", host, path, query, sign, expireHex); } else { finalUrl = string.Format("{0}{1}?sign={2}&t={3}", host, path, sign, expireHex); } return(finalUrl); }
/// <summary> /// 生成默认的断点记录文件名称 /// </summary> /// <param name="localFile">待上传的本地文件</param> /// <param name="saveKey">要保存的目标key</param> /// <returns>用于记录断点信息的文件名</returns> public static string GetDefaultRecordKey(string localFile, string saveKey) { return("QiniuRU_" + Hashing.CalcMD5X(localFile + saveKey)); }
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(); }
/// <summary> /// 多部分表单数据(multi-part form-data)的分界(boundary)标识 /// </summary> /// <returns>分界(boundary)标识字符串</returns> public static string CreateFormDataBoundary() { string now = DateTime.UtcNow.Ticks.ToString(); return(string.Format("-------{0}Boundary{1}", QiniuCSharpSDK.ALIAS, Hashing.CalcMD5X(now))); }