Exemple #1
0
        /// <summary>
        /// Mkfile the specified blkRets, key and fsize.
        /// </summary>
        /// <param name="blkRets">Blk rets.</param>
        /// <param name="key">Key.</param>
        /// <param name="fsize">Fsize.</param>
        private static byte[] Mkfile(BlkputRet[] blkRets, string token, string key, long fsize)
        {
            StringBuilder urlBuilder = new StringBuilder();

            urlBuilder.AppendFormat("{0}/mkfile/{1}", Config.UP_HOST, fsize);
            if (!string.IsNullOrEmpty(key))
            {
                urlBuilder.AppendFormat("/key/{0}", Base64URLSafe.ToBase64URLSafe(key));
            }
            int proCount = blkRets.Length;

            using (Stream body = new MemoryStream())
            {
                for (int i = 0; i < proCount; i++)
                {
                    byte[] bctx = Encoding.ASCII.GetBytes(blkRets[i].ctx);
                    body.Write(bctx, 0, bctx.Length);
                    if (i != proCount - 1)
                    {
                        body.WriteByte((byte)',');
                    }
                }
                body.Seek(0, SeekOrigin.Begin);
                byte[] data = new byte[body.Length];
                body.Read(data, 0, (int)body.Length);
                using (QiniuWebClient client = new QiniuWebClient()){
                    client.UpToken = token;
                    client.Headers.Add("Content-Type", "application/octet-stream");
                    return(client.UploadData(urlBuilder.ToString(), "POST", data));
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// Uploads the small file ( < 8MB).
        /// </summary>
        /// <param name="token">Token.</param>
        private void uploadSmallFile(string token)
        {
            if (uploading)
            {
                return;
            }
            uploading = true;
            NameValueCollection formData = new NameValueCollection();

            formData ["key"]   = this.key;
            formData ["token"] = token;
            try {
                using (QiniuWebClient qwc = new QiniuWebClient()) {
                    qwc.UploadDataCompleted += (sender, e) => {
                        if (e.Error != null && e.Error is WebException)
                        {
                            if (e.Error is WebException)
                            {
                                QiniuWebException qwe = new QiniuWebException(e.Error as WebException);
                                onUploadFailed(new QiniuUploadFailedEventArgs(qwe));
                            }
                            else
                            {
                                onUploadFailed(new QiniuUploadFailedEventArgs(new QiniuWebException(e.Error)));
                            }
                        }
                        else
                        {
                            onQiniuUploadCompleted(new QiniuUploadCompletedEventArgs(e.Result));
                        }
                    };
                    qwc.UploadProgressChanged += (sender, e) => {
                        onQiniuUploadProgressChanged(new QiniuUploadProgressChangedEventArgs(e.BytesSent, e.TotalBytesToSend));
                    };
                    qwc.AsyncPostForm(Config.UP_HOST, formData, this.localfile);
                }
            } catch (WebException we) {
                onUploadFailed(new QiniuUploadFailedEventArgs(new QiniuWebException(we)));
            } catch (Exception e) {
                onUploadFailed(new QiniuUploadFailedEventArgs(new QiniuWebException(e)));
            } finally {
                uploading = false;
            }
        }
Exemple #3
0
        /// <summary>
        /// Delete the QiniuFile with mac.
        /// </summary>
        /// <param name="mac">Mac.</param>
        public bool Delete(MAC mac = null)
        {
            if (mac == null)
            {
                mac = new MAC();
            }
            string url = string.Format("{0}/{1}/{2}", Config.RS_HOST, "delete", Base64URLSafe.Encode(this.bucketName + ":" + this.key));

            try {
                using (QiniuWebClient client = new QiniuWebClient()) {
                    client.Call(url, mac);
                    return(true);
                }
            } catch (WebException e) {
                throw new QiniuWebException(e);
            } catch (Exception e) {
                throw e;
            }
        }
Exemple #4
0
        /// <summary>
        /// Stat the QiniuFile with mac.
        /// </summary>
        /// <param name="mac">Mac.</param>
        public QiniuFileInfo Stat(MAC mac = null)
        {
            if (mac == null)
            {
                mac = new MAC();
            }
            string url = string.Format("{0}/{1}/{2}", Config.RS_HOST, "stat", Base64URLSafe.Encode(this.bucketName + ":" + this.key));

            try {
                using (QiniuWebClient client = new QiniuWebClient()) {
                    string result = client.Call(url, mac);
                    return(GetQiniuEntry(result));
                }
            } catch (WebException e) {
                throw new QiniuWebException(e);
            }catch (Exception e) {
                throw e;
            }
        }
Exemple #5
0
        /// <summary>
        /// Uploads the string.
        /// </summary>
        /// <param name="token">Token.</param>
        /// <param name="base64Content">Base64 content.</param>
        public void UploadString(string token, int fileSize, string mimeType, string base64Content)
        {
            using (QiniuWebClient qwc = new QiniuWebClient()) {
                qwc.UpToken = token;
                string url = Config.UP_HOST +
                             string.Format("/putb64/{0}/key/{1}/mimeType/{2}",
                                           fileSize,
                                           Base64URLSafe.Encode(this.key),
                                           Base64URLSafe.Encode(mimeType));

                qwc.UploadStringCompleted += (sender, e) => {
                    if (e.Error != null && e.Error is WebException)
                    {
                        if (e.Error is WebException)
                        {
                            QiniuWebException qwe = new QiniuWebException(e.Error as WebException);
                            onUploadFailed(new QiniuUploadFailedEventArgs(qwe));
                        }
                        else
                        {
                            onUploadFailed(new QiniuUploadFailedEventArgs(new QiniuWebException(e.Error)));
                        }
                    }
                    else
                    {
                        onQiniuUploadCompleted(new QiniuUploadCompletedEventArgs(e.Result));

                        onQiniuUploadCompleted(new QiniuUploadCompletedEventArgs(e.Result));
                    }
                };

                qwc.UploadProgressChanged += (sender, e) => {
                    onQiniuUploadProgressChanged(new QiniuUploadProgressChangedEventArgs(e.BytesSent, e.TotalBytesToSend));
                };

                qwc.Headers.Add("Content-Type", "application/octet-stream");
                qwc.UploadStringAsync(new Uri(url), "POST", base64Content);
            }
        }
Exemple #6
0
        /// <summary>
        /// Uploads the big file ( > 8MB ).
        /// </summary>
        /// <param name="token">Token.</param>
        private void uploadBigFile(string token, BlkputRet[] puttedBlk = null)
        {
            uploading = true;
            Action a = () => {
                FileInfo finfo      = new FileInfo(this.localfile);
                int      blockcount = block_count(finfo.Length);

                BlkputRet [] blkRets = new BlkputRet[blockcount];
                using (FileStream fs = File.OpenRead(this.localfile)) {
                    long   totalSent = 0;
                    int    readLen   = BLOCKSIZE;
                    byte[] buf       = new byte[readLen];
                    for (int i = 0; i < blockcount; i++)
                    {
                        if (puttedBlk != null && i < puttedBlk.Length)
                        {
                            blkRets[i] = puttedBlk[i];
                            totalSent += (long)blkRets[i].offset;
                            continue;
                        }
                        if (i == blockcount - 1)
                        {
                            readLen = (int)(finfo.Length - i * BLOCKSIZE);
                            buf     = new byte[readLen];
                        }
                        fs.Seek((long)i * BLOCKSIZE, SeekOrigin.Begin);
                        fs.Read(buf, 0, readLen);
                        using (QiniuWebClient client = new QiniuWebClient()) {
                            bool failed = false;
                            client.UploadDataCompleted += (sender, e) => {
                                if (e.Error != null)
                                {
                                    onQiniuUploadBlockFailed(new QiniuUploadBlockFailedEventArgs(i, e.Error));
                                    failed = true;
                                    return;
                                }
                                else
                                {
                                    blkRets [i] = GetBlkPutRet(e.Result);
                                    onQiniuUploadBlockCompleted(new QiniuUploadBlockCompletedEventArgs(i, blkRets [i]));
                                }
                            };
                            client.UploadProgressChanged += (sender, e) => {
                                onQiniuUploadProgressChanged(new QiniuUploadProgressChangedEventArgs(totalSent + e.BytesSent, finfo.Length));
                            };
                            client.Timeout += (sender, e) => {
                                onQiniuUploadBlockFailed(new QiniuUploadBlockFailedEventArgs(i, new Exception("QiniuWebClient Timeout.")));
                                failed = true;
                            };
                            client.UpToken = token;
                            client.Headers.Add("Content-Type", "application/octet-stream");
                            string url = string.Format("{0}/mkblk/{1}", Config.UP_HOST, readLen);
                            client.iUploadDataAsync(url, "POST", buf);
                            if (failed)
                            {
                                return;
                            }
                            totalSent += readLen;
                        }
                    }
                }
                try {
                    byte[] result = Mkfile(blkRets, token, this.key, finfo.Length);
                    if (result != null && result.Length > 0)
                    {
                        onQiniuUploadCompleted(new QiniuUploadCompletedEventArgs(result));
                    }
                } catch (Exception e) {
                    onUploadFailed(new QiniuUploadFailedEventArgs(new QiniuWebException(e)));
                }
            };

            a.BeginInvoke(null, null);
        }