Beispiel #1
0
        /// <summary>
        /// 上传(来自网络回复的)数据流
        /// </summary>
        public static string UpLoadStream(Stream _stream, string _saveKey)
        {
            _saveKey += ".jpg"; //保存格式
            string result_string = _QiNiuUrl + _saveKey;

            //if (Stat(_saveKey))
            //{
            //    return result_string;
            //}

            // 生成(上传)凭证时需要使用此Mac
            // 这个示例单独使用了一个Settings类,其中包含AccessKey和SecretKey
            // 实际应用中,请自行设置您的AccessKey和SecretKey
            Mac    mac     = new Mac(_AccessKey, _SecretKey);
            string bucket  = _bucket;
            string saveKey = _saveKey;

            // 上传策略,参见
            // https://developer.qiniu.com/kodo/manual/put-policy
            PutPolicy putPolicy = new PutPolicy();

            // 如果需要设置为"覆盖"上传(如果云端已有同名文件则覆盖),请使用 SCOPE = "BUCKET:KEY"
            putPolicy.Scope = bucket + ":" + saveKey;
            //putPolicy.Scope = bucket;


            // 上传策略有效期(对应于生成的凭证的有效期)
            putPolicy.SetExpires(3600);


            // 上传到云端多少天后自动删除该文件,如果不设置(即保持默认默认)则不删除
            putPolicy.DeleteAfterDays = 1;


            // 生成上传凭证,参见
            // https://developer.qiniu.com/kodo/manual/upload-token
            string jstr  = putPolicy.ToJsonString();
            string token = Auth.CreateUploadToken(mac, jstr);

            try
            {
                string url  = "http://img.ivsky.com/img/tupian/pre/201610/09/beifang_shanlin_xuejing-001.jpg";
                var    wReq = System.Net.WebRequest.Create(url) as System.Net.HttpWebRequest;
                var    resp = wReq.GetResponse() as System.Net.HttpWebResponse;
                //using (var stream = resp.GetResponseStream())
                using (var stream = _stream)
                {
                    // 请不要使用UploadManager的UploadStream方法,因为此流不支持查找(无法获取Stream.Length)
                    // 请使用FormUploader或者ResumableUploader的UploadStream方法
                    FormUploader          fu     = new FormUploader();
                    Qiniu.Http.HttpResult result = fu.UploadStream(stream, saveKey, token);

                    //代表上传成功
                    if (result.Code.ToString().Trim() == "200")
                    {
                        return(result_string);
                    }
                    else
                    {
                        return(null);
                    }
                }
            }
            catch (Exception ex)
            {
                return(result_string);
            }
        }
Beispiel #2
0
    public override void Process()
    {
        byte[] uploadFileBytes = null;
        string uploadFileName  = null;

        if (UploadConfig.Base64)
        {
            uploadFileName  = UploadConfig.Base64Filename;
            uploadFileBytes = Convert.FromBase64String(Request[UploadConfig.UploadFieldName]);
        }
        else
        {
            var file = Request.Files[UploadConfig.UploadFieldName];
            uploadFileName = file.FileName;

            if (!CheckFileType(uploadFileName))
            {
                Result.State = UploadState.TypeNotAllow;
                WriteResult();
                return;
            }
            if (!CheckFileSize(file.ContentLength))
            {
                Result.State = UploadState.SizeLimitExceed;
                WriteResult();
                return;
            }

            uploadFileBytes = new byte[file.ContentLength];
            try
            {
                file.InputStream.Read(uploadFileBytes, 0, file.ContentLength);
            }
            catch (Exception)
            {
                Result.State = UploadState.NetworkError;
                WriteResult();
            }
        }

        Result.OriginFileName = uploadFileName;

        //var savePath = PathFormatter.Format(uploadFileName, UploadConfig.PathFormat);
        //var localPath = Server.MapPath(savePath);
        //try
        //{
        //    if (!Directory.Exists(Path.GetDirectoryName(localPath)))
        //    {
        //        Directory.CreateDirectory(Path.GetDirectoryName(localPath));
        //    }
        //    File.WriteAllBytes(localPath, uploadFileBytes);
        //    Result.Url = savePath;
        //    Result.State = UploadState.Success;
        //}
        //catch (Exception e)
        //{
        //    Result.State = UploadState.FileAccessError;
        //    Result.ErrorMessage = e.Message;
        //}
        //finally
        //{
        //    WriteResult();
        //}
        try
        {
            string AK  = "aYDym1DTzZnCVH6laD7LTV6OyHah2ik3wdCkCy3w";
            string SK  = "rYP-A_bXKtP65_Hx-g7_edM8-_0xF_z0275dG20L";
            Mac    mac = new Mac(AK, SK);
            //Auth auth = new Auth(mac);
            string bucket = "mytest";
            //string saveKey = "1.png";
            //string localFile = "D:\\1.jpg";
            // 上传策略,参见
            // https://developer.qiniu.com/kodo/manual/put-policy
            PutPolicy putPolicy = new PutPolicy();
            // 如果需要设置为"覆盖"上传(如果云端已有同名文件则覆盖),请使用 SCOPE = "BUCKET:KEY"
            // putPolicy.Scope = bucket + ":" + saveKey;
            putPolicy.Scope = bucket;
            // 上传策略有效期(对应于生成的凭证的有效期)
            putPolicy.SetExpires(3600);
            // 上传到云端多少天后自动删除该文件,如果不设置(即保持默认默认)则不删除
            putPolicy.DeleteAfterDays = 1;
            // 生成上传凭证,参见
            // https://developer.qiniu.com/kodo/manual/upload-token
            string        jstr  = putPolicy.ToJsonString();
            string        token = Auth.CreateUploadToken(mac, jstr);
            UploadManager um    = new UploadManager();
            //用年月日文件内容md5.文件类型 做文件名
            string     saveKey = DateTime.Now.ToString("yyyy-MM-dd") + "/" + CommonHelper.CalcMD5(uploadFileBytes) + Path.GetExtension(uploadFileName);
            HttpResult result  = um.UploadData(uploadFileBytes, saveKey, token);
            Result.Url   = saveKey;
            Result.State = UploadState.Success;
        }
        catch (Exception e)
        {
            Result.State        = UploadState.FileAccessError;
            Result.ErrorMessage = e.Message;
        }
        finally
        {
            WriteResult();
        }
    }
Beispiel #3
0
    public override void Process()
    {
        byte[] uploadFileBytes = null;
        string uploadFileName  = null;

        if (UploadConfig.Base64)
        {
            uploadFileName  = UploadConfig.Base64Filename;
            uploadFileBytes = Convert.FromBase64String(Request[UploadConfig.UploadFieldName]);
        }
        else
        {
            var file = Request.Files[UploadConfig.UploadFieldName];
            uploadFileName = file.FileName;

            if (!CheckFileType(uploadFileName))
            {
                Result.State = UploadState.TypeNotAllow;
                WriteResult();
                return;
            }
            if (!CheckFileSize(file.ContentLength))
            {
                Result.State = UploadState.SizeLimitExceed;
                WriteResult();
                return;
            }

            uploadFileBytes = new byte[file.ContentLength];
            try
            {
                file.InputStream.Read(uploadFileBytes, 0, file.ContentLength);
            }
            catch (Exception)
            {
                Result.State = UploadState.NetworkError;
                WriteResult();
            }
        }

        Result.OriginFileName = uploadFileName;

        /*
         * //var savePath = PathFormatter.Format(uploadFileName, UploadConfig.PathFormat);
         * //var localPath = Server.MapPath(savePath);
         * try
         * {
         *
         * if (!Directory.Exists(Path.GetDirectoryName(localPath)))
         * {
         *     Directory.CreateDirectory(Path.GetDirectoryName(localPath));
         * }
         * File.WriteAllBytes(localPath, uploadFileBytes);
         * Result.Url = savePath;
         * Result.State = UploadState.Success;
         *
         *
         * }
         * catch (Exception e)
         * {
         * Result.State = UploadState.FileAccessError;
         * Result.ErrorMessage = e.Message;
         * }
         * finally
         * {
         * WriteResult();
         * }*/

        try
        {
            //Mac mac = new Mac(AccessKey,SecretKey);
            Mac    mac    = new Mac("eQN2NwQSfcJ7KaruC61V0fd2UKGQ2YbVXzcar177", "HIZtHs1WBiFvyWY3WuoUADpQmnkVrQczg3cdLzoI");
            string bucket = "zsztest";//云存储文件名
            Qiniu.Common.Config.AutoZone("eQN2NwQSfcJ7KaruC61V0fd2UKGQ2YbVXzcar177",
                                         bucket, true);
            // 上传策略,参见
            // https://developer.qiniu.com/kodo/manual/put-policy
            PutPolicy putPolicy = new PutPolicy();
            // 如果需要设置为"覆盖"上传(如果云端已有同名文件则覆盖),请使用 SCOPE = "BUCKET:KEY"
            // putPolicy.Scope = bucket + ":" + saveKey;
            putPolicy.Scope = bucket;
            // 上传策略有效期(对应于生成的凭证的有效期)
            putPolicy.SetExpires(3600);
            // 上传到云端多少天后自动删除该文件,如果不设置(即保持默认默认)则不删除
            putPolicy.DeleteAfterDays = 1;
            // 生成上传凭证,参见
            // https://developer.qiniu.com/kodo/manual/upload-token
            string        jstr  = putPolicy.ToJsonString();
            string        token = Auth.CreateUploadToken(mac, jstr);
            UploadManager um    = new UploadManager();
            //使用年/月/日/md5做文件名
            string saveKey = DateTime.Now.ToString("yyyy/MM/dd") + "/" + CommonHelper.CalcMD5(uploadFileBytes) + Path.GetExtension(uploadFileName);
            //将图片提交                            字节流       存储地址
            HttpResult result = um.UploadData(uploadFileBytes, saveKey, token);
            Result.Url   = saveKey;
            Result.State = UploadState.Success;
        }
        catch (Exception e)
        {
            Result.State        = UploadState.FileAccessError;
            Result.ErrorMessage = e.Message;
        }
        finally
        {
            WriteResult();
        }
    }
Beispiel #4
0
        public string  QiniuUplodtest()
        {
            var accessKey = "HNXvIcHqurHJAEDnVI4v2_qDGDvVlAXhipa6uVX-";
            var secretKey = "pQOAZutqyXr679mnDclCnIeaHgF0w3U-eWDFZJsp";

            // 生成(上传)凭证时需要使用此Mac
            // 这个示例单独使用了一个Settings类,其中包含AccessKey和SecretKey
            // 实际应用中,请自行设置您的AccessKey和SecretKey
            Mac    mac     = new Mac(accessKey, secretKey);
            string bucket  = "siyouku";
            string saveKey = "$(etag)";

            // 使用前请确保AK和BUCKET正确,否则此函数会抛出异常(比如code612/631等错误)
            Qiniu.Common.Config.AutoZone(accessKey, bucket, false);


            // 上传策略,参见
            // https://developer.qiniu.com/kodo/manual/put-policy
            PutPolicy putPolicy = new PutPolicy();

            // 如果需要设置为"覆盖"上传(如果云端已有同名文件则覆盖),请使用 SCOPE = "BUCKET:KEY"
            // putPolicy.Scope = bucket + ":" + saveKey;
            putPolicy.Scope   = bucket;
            putPolicy.SaveKey = saveKey;
            // 上传策略有效期(对应于生成的凭证的有效期)
            putPolicy.SetExpires(3600);
            // 上传到云端多少天后自动删除该文件,如果不设置(即保持默认默认)则不删除
            putPolicy.DeleteAfterDays = 1;

            // 生成上传凭证,参见
            // https://developer.qiniu.com/kodo/manual/upload-token
            string jstr  = putPolicy.ToJsonString();
            string token = Auth.CreateUploadToken(mac, jstr);

            try
            {
                string url  = "http://images2015.cnblogs.com/blog/208266/201509/208266-20150913215146559-472190696.jpg";
                var    wReq = System.Net.WebRequest.Create(url) as System.Net.HttpWebRequest;
                var    resp = wReq.GetResponse() as System.Net.HttpWebResponse;

                using (var stream = resp.GetResponseStream())
                {
                    //MemoryStream destination = new MemoryStream();
                    //stream.CopyTo(destination);
                    //var md5Val = destination.GetStreamMd5();

                    // 请不要使用UploadManager的UploadStream方法,因为此流不支持查找(无法获取Stream.Length)
                    // 请使用FormUploader或者ResumableUploader的UploadStream方法
                    FormUploader fu     = new FormUploader();
                    var          result = fu.UploadStream(stream, null, token);
                    // Console.WriteLine(result);
                }
            }
            catch (Exception ex)
            {
                // Console.WriteLine(ex);
            }


            return("ok");
        }
Beispiel #5
0
        /// <summary>
        /// 设置上传策略
        /// </summary>
        /// <param name="opsParam">上传策略</param>
        internal void SetPutPolicy(UploadPersistentOpsParam opsParam)
        {
            GetPutPolicy();
            _putPolicy.SaveKey = opsParam.Key;

            #region 覆盖上传

            if (!opsParam.UploadPersistentOps.IsAllowOverlap)
            {
                _putPolicy.Scope = _qiNiuConfig.Bucket;
            }
            else
            {
                _putPolicy.Scope = _qiNiuConfig.Bucket + ":" + opsParam.Key;
            }

            #endregion

            #region 带数据处理的凭证

            if (!string.IsNullOrEmpty(opsParam.UploadPersistentOps.PersistentOps))
            {
                _putPolicy.PersistentOps = opsParam.UploadPersistentOps.PersistentOps;
            }

            #endregion

            #region 设置过期时间

            _putPolicy.SetExpires(opsParam.UploadPersistentOps.ExpireInSeconds);

            #endregion

            #region 多少天后自动删除

            _putPolicy.DeleteAfterDays = opsParam.UploadPersistentOps.DeleteAfterDays;

            #endregion

            _putPolicy.FileType   = opsParam.UploadPersistentOps.FileType;
            _putPolicy.DetectMime = opsParam.UploadPersistentOps.DetectMime;
            if (!string.IsNullOrEmpty(opsParam.UploadPersistentOps.MimeLimit))
            {
                _putPolicy.MimeLimit = opsParam.UploadPersistentOps.MimeLimit;
            }
            _putPolicy.FsizeMin   = opsParam.UploadPersistentOps.FsizeMin;
            _putPolicy.FsizeLimit = opsParam.UploadPersistentOps.FsizeLimit;

            if (!string.IsNullOrEmpty(opsParam.UploadPersistentOps.ReturnUrl))
            {
                _putPolicy.ReturnUrl = opsParam.UploadPersistentOps.ReturnUrl;
            }

            if (!string.IsNullOrEmpty(opsParam.UploadPersistentOps.ReturnBody))
            {
                _putPolicy.ReturnBody = opsParam.UploadPersistentOps.ReturnBody;
            }

            if (!string.IsNullOrEmpty(opsParam.UploadPersistentOps.CallbackUrl))
            {
                _putPolicy.CallbackUrl = opsParam.UploadPersistentOps.CallbackUrl;
            }

            if (!string.IsNullOrEmpty(opsParam.UploadPersistentOps.CallbackHost))
            {
                _putPolicy.CallbackHost = opsParam.UploadPersistentOps.CallbackHost;
            }

            if (!string.IsNullOrEmpty(opsParam.UploadPersistentOps.CallbackBody))
            {
                _putPolicy.CallbackBody = opsParam.UploadPersistentOps.CallbackBody;
            }

            if (!string.IsNullOrEmpty(opsParam.UploadPersistentOps.CallbackBodyType))
            {
                _putPolicy.CallbackBodyType = opsParam.UploadPersistentOps.CallbackBodyType;
            }

            if (!string.IsNullOrEmpty(opsParam.UploadPersistentOps.PersistentNotifyUrl))
            {
                _putPolicy.PersistentNotifyUrl = opsParam.UploadPersistentOps.PersistentNotifyUrl;
            }

            if (!string.IsNullOrEmpty(opsParam.UploadPersistentOps.PersistentPipeline))
            {
                _putPolicy.PersistentPipeline = opsParam.UploadPersistentOps.PersistentPipeline;
            }
        }
Beispiel #6
0
    public override void Process()
    {
        byte[] uploadFileBytes = null;
        string uploadFileName  = null;

        if (UploadConfig.Base64)
        {
            uploadFileName  = UploadConfig.Base64Filename;
            uploadFileBytes = Convert.FromBase64String(Request[UploadConfig.UploadFieldName]);
        }
        else
        {
            var file = Request.Files[UploadConfig.UploadFieldName];
            uploadFileName = file.FileName;

            if (!CheckFileType(uploadFileName))
            {
                Result.State = UploadState.TypeNotAllow;
                WriteResult();
                return;
            }
            if (!CheckFileSize(file.ContentLength))
            {
                Result.State = UploadState.SizeLimitExceed;
                WriteResult();
                return;
            }

            uploadFileBytes = new byte[file.ContentLength];
            try
            {
                file.InputStream.Read(uploadFileBytes, 0, file.ContentLength);
            }
            catch (Exception)
            {
                Result.State = UploadState.NetworkError;
                WriteResult();
            }
        }

        Result.OriginFileName = uploadFileName;



        ////原来的
        //var savePath = PathFormatter.Format(uploadFileName, UploadConfig.PathFormat);
        //var localPath = Server.MapPath(savePath);
        //try
        //{
        //    if (!Directory.Exists(Path.GetDirectoryName(localPath)))
        //    {
        //        Directory.CreateDirectory(Path.GetDirectoryName(localPath));
        //    }
        //    File.WriteAllBytes(localPath, uploadFileBytes);
        //    Result.Url = savePath;
        //    Result.State = UploadState.Success;
        //}
        //catch (Exception e)
        //{
        //    Result.State = UploadState.FileAccessError;
        //    Result.ErrorMessage = e.Message;
        //}
        //finally
        //{
        //    WriteResult();
        //}



        ////修改保存到七牛云
        try
        {
            //Qiniu.Common.Config.SetZone(ZoneID.CN_South, true);
            Mac mac = new Mac("2lYOOiMvton1t_i2faEFLnwG3JIMZyuj5gT9mfRP", "ulQBqveHpijWlqIGWe_ObX4ahiNnYAKyYx5M-BMl");
            // 生成(上传)凭证时需要使用此Mac
            // 这个示例单独使用了一个Settings类,其中包含AccessKey和SecretKey
            // 实际应用中,请自行设置您的AccessKey和SecretKey

            string bucket = "zsztest";
            //string saveKey = "upload/gf/a.jpg";
            //用图片上传的文件民的MD5值做文件名
            ////string saveKey = "upload/gf/a.jpg";
            //string localFile = "E:\\DemoMVC\\img\\1.jpg";

            Qiniu.Common.Config.AutoZone("2lYOOiMvton1t_i2faEFLnwG3JIMZyuj5gT9mfRP", bucket, true);
            // 上传策略,参见
            // https://developer.qiniu.com/kodo/manual/put-policy
            PutPolicy putPolicy = new PutPolicy();
            // 如果需要设置为"覆盖"上传(如果云端已有同名文件则覆盖),请使用 SCOPE = "BUCKET:KEY"
            // putPolicy.Scope = bucket + ":" + saveKey;
            putPolicy.Scope = bucket;
            // 上传策略有效期(对应于生成的凭证的有效期)
            putPolicy.SetExpires(3600);
            // 上传到云端多少天后自动删除该文件,如果不设置(即保持默认默认)则不删除
            putPolicy.DeleteAfterDays = 1;
            // 生成上传凭证,参见
            // https://developer.qiniu.com/kodo/manual/upload-token
            string        jstr  = putPolicy.ToJsonString();
            string        token = Auth.CreateUploadToken(mac, jstr);
            UploadManager um    = new UploadManager();
            //算一下MD5值,上传文件的文件名
            //用 “年/月/日/文件内容md5.文件类型”做文件名
            string     saveKey = DateTime.Now.ToString("yyyy/MM/dd") + "/" + CommonHelper.CalcMD5(uploadFileBytes) + Path.GetExtension(uploadFileName);
            HttpResult result  = um.UploadData(uploadFileBytes, saveKey, token);

            Result.Url   = saveKey;
            Result.State = UploadState.Success;
        }
        catch (Exception e)
        {
            Result.State        = UploadState.FileAccessError;
            Result.ErrorMessage = e.Message;
        }
        finally
        {
            WriteResult();
        }
    }
Beispiel #7
0
        /// <summary>
        /// 上传文件并预转格式
        /// </summary>
        /// <param name="key">要转换格式的文件名</param>
        /// <param name="filePath">文件路径</param>
        /// <returns></returns>
        public string upload(string path, bool isDeleteAfterUpload = false, bool isCover = false)
        {
            string saveKey   = new FileInfo(path).Name;
            string localFile = path;
            // 上传策略,参见
            // https://developer.qiniu.com/kodo/manual/put-policy
            PutPolicy putPolicy = new PutPolicy();

            // 如果需要设置为"覆盖"上传(如果云端已有同名文件则覆盖),请使用 SCOPE = "BUCKET:KEY"
            // putPolicy.Scope = bucket + ":" + saveKey;

            // 上传策略有效期(对应于生成的凭证的有效期)
            putPolicy.SetExpires(3600);
            // 上传到云端多少天后自动删除该文件,如果不设置(即保持默认默认)则不删除
            putPolicy.DeleteAfterDays = null;
            putPolicy.InsertOnly      = 0;
            //对转码后的文件进行使用saveas参数自定义命名,也可以不指定,文件会默认命名并保存在当前空间。
            string mp3tpname = saveKey.Split('.')[0].ToString() + ".mp3";
            String urlbase64 = Base64.UrlSafeBase64Encode(Bucket + ":" + mp3tpname);// Base64URLSafe.Encode(Bucket + ":" + mp3tpname);

            //putPolicy.Scope = isCover ? $"{Bucket}:{saveKey}" : Bucket;
            putPolicy.Scope         = isCover ? $"{Bucket}:{mp3tpname}" : Bucket;
            putPolicy.PersistentOps = "avthumb/mp3/ab/128k/ar/44100/acodec/libmp3lame|saveas/" + urlbase64;
            //规定文件要在那个“工厂”进行改装,也就是队列名称!
            //put.PersistentPipeline = "LittleBai";
            //音视频转码持久化完成后,七牛的服务器会向用户发送处理结果通知。这里指定的url就是用于接收通知的接口。
            //设置了`persistentOps`,则需要同时设置此字段
            putPolicy.PersistentNotifyUrl = System.Web.HttpContext.Current.Request.MapPath("~/ResultNotifys/PersistentNotifyUrl.aspx");


            // 生成上传凭证,参见
            // https://developer.qiniu.com/kodo/manual/upload-token
            string        jstr    = putPolicy.ToJsonString();
            string        token   = Auth.CreateUploadToken(mac, jstr);
            UploadManager um      = new UploadManager();
            HttpResult    result  = um.UploadFile(localFile, saveKey, token);
            var           jResult = JsonConvert.DeserializeObject <JObject>(result.Text);

            if (jResult.Properties().Any(s => s.Name == "error"))
            {
                throw new Exception(jResult["error"].Value <string>());
            }
            if (isDeleteAfterUpload)
            {
                try
                {
                    if (System.IO.File.Exists(path))
                    {
                        System.IO.File.Delete(path);
                    }
                }
                catch (Exception)
                {
                    //删不掉就忽略
                }
            }
            string tempkey = jResult["key"].Value <string>();
            string t       = tempkey.Substring(0, tempkey.Length - 3);

            DeleteFile(tempkey);
            return(KeyToLink(t + "mp3"));
        }
Beispiel #8
0
        public string Upload(Stream stream, string fileName)
        {
            StringBuilder sbAK = new StringBuilder(60);

            iniFileHelper.GetIniString("qiniu", "AK", "", sbAK, sbAK.Capacity);
            StringBuilder sbSK = new StringBuilder(60);

            iniFileHelper.GetIniString("qiniu", "SK", "", sbSK, sbSK.Capacity);

            Mac  mac  = new Mac(sbAK.ToString(), sbSK.ToString());
            Auth auth = new Auth(mac);

            StringBuilder sbZone = new StringBuilder(60);

            iniFileHelper.GetIniString("qiniu", "Zone", "", sbZone, sbZone.Capacity);
            // 目标空间名
            string bucket = sbZone.ToString();
            // 上传策略
            PutPolicy putPolicy = new PutPolicy();

            // 设置要上传的目标空间
            putPolicy.Scope = bucket;
            // 上传策略的过期时间(单位:秒)
            putPolicy.SetExpires(3600);
            // 文件上传完毕后,1天后自动删除
            putPolicy.DeleteAfterDays = 1;

            // 生成上传凭证
            string        uploadToken = Auth.CreateUploadToken(mac, putPolicy.ToJsonString());
            StringBuilder sbBucket    = new StringBuilder(60);

            iniFileHelper.GetIniString("qiniu", "Bucket", "", sbBucket, sbBucket.Capacity);
            Zone zone = Zone.ZONE_CN_East;

            switch (sbBucket.ToString())
            {
            case "ZONE_CN_East":
                zone = Zone.ZONE_CN_East;
                break;

            case "ZONE_CN_North":
                zone = Zone.ZONE_CN_North;
                break;

            case "ZONE_CN_South":
                zone = Zone.ZONE_CN_South;
                break;

            case "ZONE_US_North":
                zone = Zone.ZONE_US_North;
                break;

            default:
                zone = Zone.ZONE_AS_Singapore;
                break;
            }
            Config config = new Config()
            {
                //// 空间对应的机房
                Zone = zone,
                // 是否使用https域名
                UseHttps = true,
                // 上传是否使用cdn加速
                UseCdnDomains = true
            };
            FormUploader  upload   = new FormUploader(config);
            HttpResult    result   = upload.UploadStream(stream, fileName, uploadToken, null);
            StringBuilder sbDomain = new StringBuilder(60);

            iniFileHelper.GetIniString("qiniu", "Domain", "", sbDomain, sbDomain.Capacity);
            imgUrRL = sbDomain.Length > 0 ? sbDomain.ToString() : imgUrRL;
            compressMsg.PwdFileUrl = imgUrRL + fileName;
            doSendMsg(compressMsg);
            if (result.Code == 200)
            {
                return("Ok");
            }
            return("fall");
        }
Beispiel #9
0
        public static async Task <bool> UploadFile(FileTransferTask task)
        {
            var resumeDirectory = Path.Combine(ApplicationData.Current.TemporaryFolder.Path, "upload", "resume");

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

            var resumeRecordFilePath = Path.Combine(resumeDirectory, task.FileObject.FileName);
            var tempFolder           = await StorageFolder.GetFolderFromPathAsync(resumeDirectory);

            var resumeRecordFile = await tempFolder.TryGetItemAsync(task.FileObject.FileName);

            if (resumeRecordFile == null)
            {
                await tempFolder.CreateFileAsync(task.FileObject.FileName, CreationCollisionOption.ReplaceExisting);
            }

            return(await Task.Factory.StartNew(async() =>
            {
                void ProgressHandler(long uploadBytes, long totalBytes)
                {
                    var percent = uploadBytes * 1.0 / totalBytes;

                    DispatcherHelper.ExecuteOnUIThreadAsync(() =>
                    {
                        task.Progress = percent;
                    });
                }

#if MOCK
                int total = 100;
                for (int i = 0; i < 100; i++)
                {
                    ProgressHandler(i, total);
                    await Task.Delay(2000);
                }

                return true;
#else
                var uploadManager = new UploadManager(_config);
                var putExtra = new PutExtra
                {
                    ResumeRecordFile = resumeRecordFilePath,
                    BlockUploadThreads = 1,
                    ProgressHandler = ProgressHandler,
                    UploadController = delegate
                    {
                        if (task.TransferState == TransferState.Aborted)
                        {
                            return UploadControllerAction.Aborted;
                        }

                        return task.TransferState == TransferState.Suspended
                            ? UploadControllerAction.Suspended
                            : UploadControllerAction.Activated;
                    }
                };

                var putPolicy = new PutPolicy
                {
                    Scope = task.BucketObject.Name
                };
                putPolicy.SetExpires(24 * 30 * 3600);

                var auth = new Auth(_currentMac);
                var uptoken = auth.CreateUploadToken(putPolicy.ToJsonString());

                var fileStorage = await StorageApplicationPermissions.FutureAccessList.GetFileAsync(task.FileObject.FileName);
                var currentFileStream = await fileStorage.OpenStreamForReadAsync();
                var uploadResult = uploadManager.UploadStream(
                    currentFileStream,
                    task.FileObject.FileName,
                    uptoken,
                    putExtra);

                return uploadResult.Code == 200;
#endif
            }).Unwrap());
        }
Beispiel #10
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;
            Mac  mac             = new Mac(SystemConfig.ACCESS_KEY, SystemConfig.SECRET_KEY);

            //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, new 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);

            bool uploadByCdn = true;

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

            default:
                uploadByCdn = true; break;
            }

            ChunkUnit chunkSize = ChunkUnit.U4096K;

            switch (this.syncSetting.DefaultChunkSize)
            {
            case 0:
                chunkSize = ChunkUnit.U128K; break;

            case 1:
                chunkSize = ChunkUnit.U256K; break;

            case 2:
                chunkSize = ChunkUnit.U512K; break;

            case 3:
                chunkSize = ChunkUnit.U1024K; break;

            case 4:
                chunkSize = ChunkUnit.U2048K; break;

            case 5:
                chunkSize = ChunkUnit.U4096K; break;

            default:
                chunkSize = ChunkUnit.U4096K; break;
            }

            string resumeFile = System.IO.Path.Combine(recordPath, recorderKey);
            Config config     = new Config();

            config.ChunkSize     = chunkSize;
            config.UseCdnDomains = uploadByCdn;
            config.PutThreshold  = this.syncSetting.ChunkUploadThreshold;

            UploadManager uploadManager = new UploadManager(config);

            PutExtra putExtra = new PutExtra();

            putExtra.ResumeRecordFile = resumeFile;

            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;

            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.Text);
                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();
            }
            this.doneEvent.Set();
            return;
        }
Beispiel #11
0
        public async Task <IActionResult> Upload([FromServices] IHostingEnvironment environment, IFormCollection formCollection, [FromForm] FileUploadModel uploadModel)
        {
            var response = new ResponseModel();

            foreach (var formFile in formCollection.Files)
            {
                uploadModel.ContentType = formFile.ContentType.Replace("\\", "").Replace("/", "");
                //创建文件名称
                //string fileName = $"{DescHelper.DescEncrypt($"{JsonHelper.Json(uploadModel)}")}" +
                //    $"{Path.GetExtension(formFile.FileName)}";
                string fileName = $"{Guid.NewGuid().ToString()}" +
                                  $"{Path.GetExtension(formFile.FileName)}";
                //七牛云存储
                if (uploadModel.StorageMethod.Equals("qiniuyun"))//七牛云存储
                {
                    var    stream = formFile.OpenReadStream();
                    byte[] data   = new byte[stream.Length];
                    await stream.ReadAsync(data, 0, data.Length);

                    Mac mac = new Mac(_configuration.Value.AccessKey, _configuration.Value.SecretKey);
                    //设置使用七牛云空间
                    string bucket = uploadModel.FileSpaceType.Equals("public") ? [email protected](',')[0] : [email protected](',')[0];
                    // 上传策略,参见
                    // https://developer.qiniu.com/kodo/manual/put-policy
                    PutPolicy putPolicy = new PutPolicy();
                    // 如果需要设置为"覆盖"上传(如果云端已有同名文件则覆盖),请使用 SCOPE = "BUCKET:KEY"
                    putPolicy.Scope = bucket + ":" + fileName;
                    // 上传策略有效期(对应于生成的凭证的有效期)
                    putPolicy.SetExpires(3600);
                    putPolicy.ReturnBody = "$(key)";
                    // 上传到云端多少天后自动删除该文件,如果不设置(即保持默认默认)则不删除
                    //putPolicy.DeleteAfterDays = 1;
                    // 生成上传凭证,参见
                    // https://developer.qiniu.com/kodo/manual/upload-token
                    string jstr  = putPolicy.ToJsonString();
                    string token = Auth.CreateUploadToken(mac, jstr);
                    Config.AutoZone(mac.AccessKey, bucket, false);
                    FormUploader fu     = new FormUploader();
                    HttpResult   result = await fu.UploadDataAsync(data, fileName, token);

                    if (result.Code == 200)
                    {
                        response.RetMsg  = "文件上传成功";
                        response.RetCode = StatesCode.success;
                        response.Data    = new
                        {
                            FileAddress = $"{[email protected](',')[1]}/{result.Text.Replace("\"", "")}"
                        };//文件地址
                    }
                }
                //服务器本地存储
                else if (uploadModel.StorageMethod.Equals("bendi"))
                {
                    var filePath = $@"{environment.ContentRootPath}\Upload\{uploadModel.ContentType}";
                    if (!Directory.Exists(filePath))
                    {
                        Directory.CreateDirectory(filePath);
                    }
                    using (var stream = new FileStream(Path.Combine(filePath, fileName), FileMode.CreateNew))
                    {
                        await formFile.CopyToAsync(stream);
                    }
                    response.RetMsg  = "文件上传成功";
                    response.RetCode = StatesCode.success;
                    response.Data    = new
                    {
                        FileAddress = new StringBuilder()
                                      .Append(Request.Scheme)
                                      .Append("://")
                                      .Append(Request.Host)
                                      .Append($"/{uploadModel.ContentType}/")
                                      .ToString() + fileName
                    };//文件地址
                }
            }
            return(Ok(response));
        }
Beispiel #12
0
        public static bool UploadFileData(string filename, byte[] data, string filepath)
        {
            // // 上传文件名
            string key = filepath + filename;
            // // 本地文件路径
            // string filePath = filepath;
            // // 存储空间名
            // string Bucket = senparcQiniuSetting.QiniuOSSPictureBucket;
            // // 设置上传策略,详见:https://developer.qiniu.com/kodo/manual/1206/put-policytouchun
            // PutPolicy putPolicy = new PutPolicy();

            // putPolicy.Scope = Bucket;
            ////    putPolicy.Scope = Bucket;
            // putPolicy.SetExpires(3600);
            // //putPolicy.DeleteAfterDays = 1;
            // string token = Auth.CreateUploadToken(mac, putPolicy.ToJsonString());
            // Config config = new Config();
            // // 设置上传区域
            // Qiniu.Common.Config.AutoZone(senparcQiniuSetting.QiniuAccessKeyId, Bucket, true);

            // Qiniu.CDN.CdnManager cdn = new Qiniu.CDN.CdnManager(mac);
            //  // 表单上传
            //  FormUploader target = new FormUploader(true);

            // //HttpResult result = target.UploadData(data, key, token, null);
            // HttpResult result = target.UploadData(data, key, token);
            // if (result != null && result.Code == 200)
            // {
            //     return true;
            // }

            // else
            // {
            //     return false;
            // }
            // // HttpResult result = target.UploadFile(filePath, key, token, null);
            // Console.WriteLine("form upload result: " + result.ToString());

            // 生成(上传)凭证时需要使用此Mac
            // 这个示例单独使用了一个Settings类,其中包含AccessKey和SecretKey
            // 实际应用中,请自行设置您的AccessKey和SecretKey
            Mac mac = new Mac(senparcQiniuSetting.QiniuAccessKeyId, senparcQiniuSetting.QiniuOSSAccessKeySecret);
            //string bucket = "test";
            //string saveKey = "myfile";
            // byte[] data = System.IO.File.ReadAllBytes("D:/QFL/1.mp3");
            //byte[] data = System.Text.Encoding.UTF8.GetBytes("Hello World!");
            // 上传策略,参见
            // https://developer.qiniu.com/kodo/manual/put-policy
            PutPolicy putPolicy = new PutPolicy();

            // 如果需要设置为"覆盖"上传(如果云端已有同名文件则覆盖),请使用 SCOPE = "BUCKET:KEY"
            // putPolicy.Scope = bucket + ":" + saveKey;
            putPolicy.Scope = senparcQiniuSetting.QiniuOSSPictureBucket;
            Qiniu.Common.Config.AutoZone(senparcQiniuSetting.QiniuAccessKeyId, senparcQiniuSetting.QiniuOSSPictureBucket, true);
            // 上传策略有效期(对应于生成的凭证的有效期)
            putPolicy.SetExpires(3600);
            // 上传到云端多少天后自动删除该文件,如果不设置(即保持默认默认)则不删除
            // putPolicy.DeleteAfterDays = 1;
            // 生成上传凭证,参见

            // https://developer.qiniu.com/kodo/manual/upload-token
            string       jstr   = putPolicy.ToJsonString();
            string       token  = Auth.CreateUploadToken(mac, jstr);
            FormUploader fu     = new FormUploader();
            HttpResult   result = fu.UploadData(data, key, token);

            if (result != null && result.Code == 200)
            {
                return(true);
            }

            else
            {
                return(false);
            }
        }
Beispiel #13
0
        private void CreateTokenButton_Click(object sender, RoutedEventArgs e)
        {
            string scope  = this.ScopeTextBox.Text.Trim();
            int    expire = 3600;

            if (string.IsNullOrEmpty(scope))
            {
                TextBox_UploadResultText.Text = "配置出错,请输入正确的Scope\r\n";
                return;
            }

            bool ok = int.TryParse(this.ExpireTextBox.Text.Trim(), out expire);

            if (!ok || expire < 0)
            {
                TextBox_UploadResultText.Text = "配置出错,请输入正确的expire\r\n";
                return;
            }

            int    insertOnly            = this.InsertOnlyCheckBox.IsChecked.Value ? 1 : 0;
            string saveKey               = this.SaveKeyTextBox.Text;
            string endUser               = this.EndUserTextBox.Text;
            string returnBody            = this.ReturnBodyTextBox.Text;
            string callbackUrl           = this.CallbackUrlTextBox.Text;
            string callbackHost          = this.CallbackHostTextBox.Text;
            string callbackBodyType      = "";
            int    callbackBodyTypeIndex = this.CallbackBodyTypeComboBox.SelectedIndex;

            if (callbackBodyTypeIndex != -1)
            {
                ComboBoxItem item = (ComboBoxItem)this.CallbackBodyTypeComboBox.SelectedItem;
                callbackBodyType = item.Content.ToString();
            }
            string callbackBody        = this.CallbackBodyTextBox.Text;
            int    callbackFetchKey    = this.CallbackFetchKeyCheckBox.IsChecked.Value ? 1 : 0;
            string persistentOps       = this.PersistentOpsTextBox.Text;
            string persistentPipeline  = this.PersistentPipelineTextBox.Text;
            string persistentNotifyUrl = this.PersistentNotifyUrlTextBox.Text;
            int    fsizeLimit          = -1;

            try
            {
                fsizeLimit = Convert.ToInt32(this.FsizeLimitTextBox.Text.Trim());
            }
            catch (Exception) { }
            //////////////////////////////////////////////////////////////////////////////////////////////////////
            //
            // 如果用户设置了mimeType
            //     mimeType = MimeTypeTextBox.Text
            //     PutPolicy.detectMime = 0
            //
            // 如果用户未设置mimeType
            //     mimeType = application/octect-stream
            //     PutPolicy.detectMime = DetectMimeCheckBox.IsChecked
            //
            //////////////////////////////////////////////////////////////////////////////////////////////////////
            int detectMime = 0;

            mimeType = this.MimeTypeTextBox.Text.Trim();
            if (mimeType.Length == 0)
            {
                mimeType   = "application/octet-stream";
                detectMime = this.DetectMimeCheckBox.IsChecked.Value ? 1 : 0;
            }

            string mimeLimit = this.MimeLimitTextBox.Text;

            int  deleteAfterDays   = -1;
            bool deleteAfterDaysOK = int.TryParse(this.DeleteAfterDaysTextBox.Text.Trim(), out deleteAfterDays);

            if (deleteAfterDaysOK && deleteAfterDays <= 0)
            {
                TextBox_UploadResultText.Text = "配置出错,请输入正确的deleteAfterDays\r\n";
                return;
            }

            PutPolicy putPolicy = new PutPolicy();

            putPolicy.Scope = scope;
            putPolicy.SetExpires(expire);
            if (insertOnly != 0)
            {
                putPolicy.InsertOnly = insertOnly;
            }
            if (!string.IsNullOrEmpty(saveKey))
            {
                putPolicy.SaveKey = saveKey;
            }
            if (!string.IsNullOrEmpty(endUser))
            {
                putPolicy.EndUser = endUser;
            }
            if (!string.IsNullOrEmpty(returnBody))
            {
                putPolicy.ReturnBody = returnBody;
            }
            if (!string.IsNullOrEmpty(callbackUrl))
            {
                putPolicy.CallbackUrl = callbackUrl;
            }
            if (!string.IsNullOrEmpty(callbackHost))
            {
                putPolicy.CallbackHost = callbackHost;
            }
            if (!string.IsNullOrEmpty(callbackBody))
            {
                putPolicy.CallbackBody = callbackBody;
            }
            if (!string.IsNullOrEmpty(callbackBodyType))
            {
                putPolicy.CallbackBodyType = callbackBodyType;
            }
            if (callbackFetchKey != 0)
            {
                putPolicy.CallbackFetchKey = callbackFetchKey;
            }
            if (!string.IsNullOrEmpty(persistentOps))
            {
                putPolicy.PersistentOps = persistentOps;
            }
            if (!string.IsNullOrEmpty(persistentPipeline))
            {
                putPolicy.PersistentPipeline = persistentPipeline;
            }
            if (!string.IsNullOrEmpty(persistentNotifyUrl))
            {
                putPolicy.PersistentNotifyUrl = persistentNotifyUrl;
            }
            if (fsizeLimit > 0)
            {
                putPolicy.FsizeLimit = fsizeLimit;
            }
            if (!string.IsNullOrEmpty(mimeLimit))
            {
                putPolicy.MimeLimit = mimeLimit;
            }
            if (detectMime != 0)
            {
                putPolicy.DetectMime = detectMime;
            }
            if (deleteAfterDays > 0)
            {
                putPolicy.DeleteAfterDays = deleteAfterDays;
            }
            string accessKey = QiniuLab.AppSettings.Default.ACCESS_KEY;
            string secretKey = QiniuLab.AppSettings.Default.SECRET_KEY;

            #region FIX_ADD_ZONE_CONFIG
            try
            {
                string bucket = scope;
                int    pos    = scope.IndexOf(':');
                if (pos > 0)
                {
                    bucket = scope.Remove(pos);
                }

                Config.AutoZone(AppSettings.Default.ACCESS_KEY, bucket, false);
                TextBox_UploadResultText.Clear();
            }
            catch (Exception ex)
            {
                TextBox_UploadResultText.Text = "配置出错,请检查您的输入(如密钥/scope/bucket等)\r\n" + ex.Message;
                return;
            }
            #endregion FIX_ADD_ZONE_CONFIG
            Mac    mac         = new Mac(accessKey, secretKey);
            string uploadToken = Qiniu.Util.Auth.CreateUploadToken(mac, putPolicy.ToJsonString());
            this.UploadTokenTextBox.Text = uploadToken;
        }
Beispiel #14
0
        public void uploadFile(object obj)
        {
            FileItem item = obj as FileItem;

            if (syncProgressPage.checkCancelSignal())
            {
                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)
            {
                this.syncProgressPage.updateUploadLog("上传成功 " + fileFullPath);
                this.syncProgressPage.addFileUploadSuccessLog(string.Format("{0}\t{1}\t{2}", this.syncSetting.TargetBucket,
                                                                            fileFullPath, item.SaveKey));
                this.syncProgressPage.updateTotalUploadProgress();
            }
            else
            {
                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();
        }
Beispiel #15
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
            Qiniu.Common.Config.PUT_THRESHOLD = this.syncSetting.ChunkUploadThreshold;
            Qiniu.Common.Config.CHUNK_SIZE    = this.syncSetting.DefaultChunkSize;

            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;
            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.SyncLocalDir,
                                               this.syncSetting.SyncTargetBucket, fileKey, fileFullPath, fileLastModified);

            recorderKey = Tools.md5Hash(recorderKey);

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

                if (!string.IsNullOrEmpty(statResult.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 = QETag.hash(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 = QETag.hash(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 = QETag.hash(fileFullPath);
                    }

                    if (localHash.Equals(statResult.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));
                }

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

            //if file not exists or need to overwrite
            this.syncProgressPage.updateUploadLog("准备上传文件 " + fileFullPath);
            UploadManager uploadManger = new UploadManager(new Qiniu.Storage.Persistent.ResumeRecorder(recordPath),
                                                           new Qiniu.Storage.Persistent.KeyGenerator(delegate() { return(recorderKey); }));
            PutPolicy putPolicy = new PutPolicy();

            if (overwriteUpload)
            {
                putPolicy.Scope = this.syncSetting.SyncTargetBucket + ":" + fileKey;
            }
            else
            {
                putPolicy.Scope = this.syncSetting.SyncTargetBucket;
            }
            putPolicy.SetExpires(24 * 30 * 3600);
            string uptoken = Auth.createUploadToken(putPolicy, mac);

            this.syncProgressPage.updateUploadLog("开始上传文件 " + fileFullPath);
            uploadManger.uploadFile(fileFullPath, fileKey, uptoken, new UploadOptions(null, null, false,
                                                                                      new UpProgressHandler(delegate(string key, double percent)
            {
                this.syncProgressPage.updateSingleFileProgress(taskId, fileFullPath, fileKey, fileLength, percent);
            }), new UpCancellationSignal(delegate()
            {
                return(this.syncProgressPage.checkCancelSignal());
            }))
                                    , new UpCompletionHandler(delegate(string key, ResponseInfo respInfo, string response)
            {
                if (respInfo.StatusCode != 200)
                {
                    this.syncProgressPage.updateUploadLog("上传失败 " + fileFullPath + "," + respInfo.Error);
                    this.syncProgressPage.addFileUploadErrorLog(string.Format("{0}\t{1}\t{2}\t{3}", this.syncSetting.SyncTargetBucket,
                                                                              fileFullPath, fileKey, respInfo.Error + "" + response));

                    //file exists error
                    if (respInfo.StatusCode == 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>(response);
                        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();
                }

                this.doneEvent.Set();
            }));
        }