Пример #1
0
        public void UploadFile(string folder,
                               string fileName, int category, byte[] bytes, string token,
                               IProgress <ProgressInfo> callback)
        {
            if (string.IsNullOrEmpty(token))
            {
                throw new ArgumentException("Token is empty.");
            }

            if (null == bytes || bytes.Length < 1)
            {
                throw new FormatException("Size of " + fileName + " is zero.");
            }

            if (bytes.Length > CloudUtils.SIZE_LIMIT && category != CloudUtils.CATEGORY_MOBILE_RESOURCES)
            {
                throw new FormatException("Size of " + fileName +
                                          " is over 30MB. Size:" + bytes.Length);
            }

            Stream stream = null;

            try
            {
                stream = CloudUtils.BytesToStream(bytes);
            }
            catch (Exception e)
            {
                throw e;
            }

            if (null == stream || 0 == stream.Length)
            {
                throw new ArgumentException("Data stream is null");
            }

            var progress = new Progress <ResponseInfo>();

            progress.ProgressChanged += (o, info) =>
            {
                if (info.Filename == fileName &&
                    !string.IsNullOrEmpty(info.ResponseContent))
                {
                    Task.Run(async() => await PutHttpResponse(fileName,
                                                              info.ResponseContent, stream,
                                                              token, callback, 0, null));
                    return;
                }
                Log.Instance.E(TAG, "Filename : " + info.Filename +
                               ", ResponseContent=" + info.ResponseContent);
                var args = new ProgressInfo(fileName, 0, 1)
                {
                    Error = ERROR_GENERAL_NULL
                };
                callback.Report(args);
            };
            Task.Run(async() =>
                     await GetHttpResponse(fileName, GetHttpUrl(TYPE_UPLOAD,
                                                                folder, category, fileName), token, progress, 0));
        }
Пример #2
0
 public void UploadFile(string folder,
                        string fileName, int category, Stream stream, string token,
                        IProgress <ProgressInfo> callback)
 {
     byte[] bytes;
     try
     {
         bytes = CloudUtils.StreamToBytes(stream);
     }
     catch (Exception e)
     {
         throw e;
     }
     UploadFile(folder, fileName, category, bytes, token, callback);
 }
Пример #3
0
        private string GetHttpUrl(string httpType,
                                  string folder, int category, string fileName)
        {
            string domain = CloudUtils.GetCloudDomain(_environment);

            if (string.IsNullOrEmpty(folder))
            {
                throw new ArgumentException("Folder is empty.");
            }
            if (string.IsNullOrEmpty(fileName))
            {
                throw new ArgumentException("FileName is empty.");
            }
            Log.Instance.D(TAG, "GetUrl accountEnv=" + _environment +
                           ", category=" + category + ", fileName=" + fileName);
            UriBuilder builder = new UriBuilder(domain)
            {
                Path  = "api/v1/urls/" + httpType,
                Query = "?category=" + category +
                        "&folder=" + folder + "&filename=" + fileName
            };

            return(builder.Uri.AbsoluteUri);
        }