예제 #1
0
        private MediaUploadResult UploadChunkedImpl(Stream media, int totalBytes, UploadMediaType mediaType, IEnumerable <KeyValuePair <string, object> > parameters)
        {
            var mediaId = this.UploadInitCommandImpl(
                parameters.EndWith(
                    new KeyValuePair <string, object>("total_bytes", totalBytes),
                    new KeyValuePair <string, object>("media_type", mediaType)
                    ))
                          .MediaId;

            const int maxChunkSize   = 5 * 1000 * 1000;
            var       chunk          = new byte[maxChunkSize];
            var       remainingBytes = totalBytes;

            for (var segmentIndex = 0; remainingBytes > 0; segmentIndex++)
            {
                var readCount = media.Read(chunk, 0, Math.Min(remainingBytes, maxChunkSize));
                if (readCount == 0)
                {
                    break;
                }
                this.UploadAppendCommand(new Dictionary <string, object>
                {
                    { "media_id", mediaId },
                    { "segment_index", segmentIndex },
                    { "media", new ArraySegment <byte>(chunk, 0, readCount) }
                });
                remainingBytes -= readCount;
            }

            return(this.UploadFinalizeCommand(mediaId));
        }
예제 #2
0
파일: Media.cs 프로젝트: cannorin/CoreTweet
        private MediaUploadResult UploadChunkedImpl(Stream media, int totalBytes, UploadMediaType mediaType, IEnumerable <KeyValuePair <string, object> > parameters)
        {
            string mediaId;

            using (var res = AccessUploadApi(new Dictionary <string, object>()
            {
                { "command", "INIT" },
                { "total_bytes", totalBytes },
                { "media_type", GetMediaTypeString(mediaType) }
            }.Concat(parameters)))
                using (var sr = new StreamReader(res.GetResponseStream()))
                    mediaId = (string)JObject.Parse(sr.ReadToEnd())["media_id_string"];

            const int maxChunkSize = 5 * 1000 * 1000;

            byte[] chunk          = null;
            var    remainingBytes = totalBytes;

            for (var segmentIndex = 0; remainingBytes > 0; segmentIndex++)
            {
                var chunkSize = remainingBytes < maxChunkSize ? remainingBytes : maxChunkSize;
                if (chunk == null || chunk.Length != chunkSize)
                {
                    chunk = new byte[chunkSize];
                }
                var readCount = media.Read(chunk, 0, chunkSize);
                if (readCount == 0)
                {
                    break;
                }
                if (chunkSize != readCount)
                {
                    var newChunk = new byte[readCount];
                    Buffer.BlockCopy(chunk, 0, newChunk, 0, readCount);
                    chunk = newChunk;
                }
                this.AccessUploadApi(new Dictionary <string, object>()
                {
                    { "command", "APPEND" },
                    { "media_id", mediaId },
                    { "segment_index", segmentIndex },
                    { "media", chunk }
                }).Close();
                remainingBytes -= readCount;
            }

            using (var res = AccessUploadApi(new Dictionary <string, object>()
            {
                { "command", "FINALIZE" },
                { "media_id", mediaId }
            }))
                using (var sr = new StreamReader(res.GetResponseStream()))
                {
                    var json   = sr.ReadToEnd();
                    var result = CoreBase.Convert <MediaUploadResult>(json);
                    result.Json = json;
                    return(result);
                }
        }
예제 #3
0
파일: Media.cs 프로젝트: cannorin/CoreTweet
        /// <summary>
        /// Uploads videos or chunked images to Twitter for use in a Tweet or Twitter-hosted Card.
        /// </summary>
        /// <param name="media">The raw binary file content being uploaded.</param>
        /// <param name="totalBytes">The size of the media being uploaded in bytes.</param>
        /// <param name="mediaType">The type of the media being uploaded.</param>
        /// <param name="additional_owners">A comma-separated string of user IDs to set as additional owners who are allowed to use the returned media_id in Tweets or Cards.</param>
        /// <returns>The result for the uploaded media.</returns>
        public MediaUploadResult UploadChunked(Stream media, int totalBytes, UploadMediaType mediaType, IEnumerable <long> additional_owners = null)
        {
            var parameters = new Dictionary <string, object>();

            if (additional_owners != null)
            {
                parameters.Add(nameof(additional_owners), additional_owners);
            }
            return(this.UploadChunkedImpl(media, totalBytes, mediaType, parameters));
        }
예제 #4
0
        /// <summary>
        /// 上传临时素材文件
        /// 所有文件size必须大于5个字节
        /// 图片(image):2MB,支持JPG,PNG格式
        /// 语音(voice):2MB,播放长度不超过60s,支持AMR格式
        /// 视频(video):10MB,支持MP4格式
        /// 普通文件(file):20MB
        /// </summary>
        /// <param name="accessToken">调用接口凭证</param>
        /// <param name="fileFullName">文件名称(包含文件路径)</param>
        /// <param name="fileStream">文件流</param>
        /// <param name="mediaType">媒体文件类型</param>
        public MediaUploadResult UploadTemp(string accessToken, string fileFullName, Stream fileStream, UploadMediaType mediaType)
        {
            //string resultJson = HttpHelper.MaterialUploads(EntRequestUrl.UploadMedia.ToUrl(accessToken, mediaType.ToString()), fileName, mediaType.ToString(), fileStream);
            //MediaUploadResult result = JsonConvert.DeserializeObject<MediaUploadResult>(resultJson);
            //return result;

            var fileDictionary = new Dictionary<string, string>();
            fileDictionary["media"] = fileFullName;
            string resultJson = HttpHelper.PostFileGetJson(EntRequestUrl.UploadMedia.ToUrl(accessToken, mediaType.ToString()), null, fileDictionary, null, null);
            MediaUploadResult result = JsonConvert.DeserializeObject<MediaUploadResult>(resultJson);
            return result;
        }
예제 #5
0
        private MediaUploadResult UploadChunkedImpl(Stream media, long totalBytes, UploadMediaType mediaType, IEnumerable <KeyValuePair <string, object> > parameters, string urlPrefix, string urlSuffix)
        {
            var mediaId = this.UploadInitCommandImpl(
                parameters.EndWith(
                    new KeyValuePair <string, object>("total_bytes", totalBytes),
                    new KeyValuePair <string, object>("media_type", mediaType)
                    ), urlPrefix, urlSuffix)
                          .MediaId;

            const int maxChunkSize   = 5 * 1000 * 1000;
            var       chunk          = new byte[maxChunkSize];
            var       remainingBytes = totalBytes;

            for (var segmentIndex = 0; remainingBytes > 0; segmentIndex++)
            {
                var readCount = media.Read(chunk, 0, (int)Math.Min(remainingBytes, maxChunkSize));
                if (readCount == 0)
                {
                    break;
                }
                this.UploadAppendCommand(new Dictionary <string, object>
                {
                    { "media_id", mediaId },
                    { "segment_index", segmentIndex },
                    { "media", new ArraySegment <byte>(chunk, 0, readCount) }
                });
                remainingBytes -= readCount;
            }

            var result = this.UploadFinalizeCommand(mediaId);

            while (result.ProcessingInfo?.CheckAfterSecs != null)
            {
                Thread.Sleep(result.ProcessingInfo.CheckAfterSecs.Value * 1000);
                result = this.UploadStatusCommand(mediaId);
            }

            if (result.ProcessingInfo?.State == "failed")
            {
                throw new MediaProcessingException(result);
            }

            return(result);
        }
예제 #6
0
        /// <summary>
        /// 上传媒体文件。
        /// 指定文件路径、文件格式和有效的访问令牌,将文件发送给微信服务器。
        ///
        /// 注意:微信服务器只保存3天。
        /// </summary>
        /// <param name="accessToken">访问令牌</param>
        /// <param name="type">文件类型。支持JPG, MP3, MP4, AMR等格式,详见<see cref="UploadMediaType"/></param>
        /// <param name="file">文件路径</param>
        /// <returns>若成功,则返回上传成功的时间、文件格式、以及资源ID;若失败,则抛出异常。</returns>
        /// <exception cref="WeixinException">下载不成功,则抛出该异常</exception>
        public static async Task <MediaUploadResultJson> Upload(string accessToken, UploadMediaType type, string file)
        {
            string api = "http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESSTOKEN&UploadMediaType=UPLOADMEDIATYPE";

            api = api.Replace("ACCESSTOKEN", accessToken);
            api = api.Replace("UPLOADMEDIATYPE", type.ToString());

            //var fileDictionary = new Dictionary<string, string>();
            //fileDictionary["media"] = file;
            //return HttpUtilityPost.PostFileGetJson<MediaUploadResultJson>(url, null, fileDictionary, null);
            var httpResponseMessage = await HttpUtility.UploadFile(api, file);

            byte[] responseData = await httpResponseMessage.Content.ReadAsByteArrayAsync();

            string resp = Encoding.UTF8.GetString(responseData);

            var json = resp.FromJson <MediaUploadResultJson>();

            return(json);
        }
예제 #7
0
        /// <summary>
        /// Uploads videos or chunked images to Twitter for use in a Tweet or Twitter-hosted Card as an asynchronous operation.
        /// </summary>
        /// <param name="media">The raw binary file content being uploaded.</param>
        /// <param name="totalBytes">The size of the media being uploaded in bytes.</param>
        /// <param name="mediaType">The type of the media being uploaded.</param>
        /// <param name="additional_owners">A comma-separated string of user IDs to set as additional owners who are allowed to use the returned media_id in Tweets or Cards.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>The result for the uploaded media.</returns>
        public Task <MediaUploadResult> UploadChunkedAsync(Stream media, int totalBytes, UploadMediaType mediaType, IEnumerable <long> additional_owners = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            var parameters = new Dictionary <string, object>();

            if (additional_owners != null)
            {
                parameters.Add(nameof(additional_owners), additional_owners);
            }
            return(this.UploadChunkedAsyncImpl(media, totalBytes, mediaType, parameters, cancellationToken));
        }
예제 #8
0
 /// <summary>
 /// <para>Uploads videos or chunked images to Twitter for use in a Tweet or Twitter-hosted Card as an asynchronous operation.</para>
 /// <para>Available parameters:</para>
 /// <para>- <c>IEnumerbale&lt;long&gt;</c> additional_owners (optional)</para>
 /// </summary>
 /// <param name="media">The raw binary file content being uploaded.</param>
 /// <param name="totalBytes">The size of the media being uploaded in bytes.</param>
 /// <param name="mediaType">The type of the media being uploaded.</param>
 /// <param name="parameters">The parameters.</param>
 /// <param name="cancellationToken">The cancellation token.</param>
 /// <returns>
 /// <para>The task object representing the asynchronous operation.</para>
 /// <para>The Result property on the task object returns the result for the uploaded media.</para>
 /// </returns>
 public Task <MediaUploadResult> UploadChunkedAsync(Stream media, int totalBytes, UploadMediaType mediaType, IDictionary <string, object> parameters, CancellationToken cancellationToken = default(CancellationToken))
 {
     return(this.UploadChunkedAsyncImpl(media, totalBytes, mediaType, parameters, cancellationToken));
 }
예제 #9
0
        private Task <MediaUploadResult> UploadChunkedAsyncImpl(Stream media, int totalBytes, UploadMediaType mediaType, IEnumerable <KeyValuePair <string, object> > parameters, CancellationToken cancellationToken)
        {
            return(this.AccessUploadApiAsync(
                       new Dictionary <string, object>()
            {
                { "command", "INIT" },
                { "total_bytes", totalBytes },
                { "media_type", GetMediaTypeString(mediaType) }
            }.Concat(parameters), cancellationToken)
                   .ReadResponse(s => (string)JObject.Parse(s)["media_id_string"], cancellationToken)
                   .Done(mediaId =>
            {
                var tasks = new List <Task>();
                const int maxChunkSize = 5 * 1000 * 1000;
                var remainingBytes = totalBytes;

                for (var i = 0; remainingBytes > 0; i++)
                {
                    var segmentIndex = i;     // to capture the variable
                    var chunkSize = remainingBytes < maxChunkSize ? remainingBytes : maxChunkSize;
                    var chunk = new byte[chunkSize];
                    var readCount = media.Read(chunk, 0, chunkSize);
                    if (readCount == 0)
                    {
                        break;
                    }
                    if (chunkSize != readCount)
                    {
                        var newChunk = new byte[readCount];
                        Buffer.BlockCopy(chunk, 0, newChunk, 0, readCount);
                        chunk = newChunk;
                    }
                    remainingBytes -= readCount;
                    tasks.Add(
                        this.AccessUploadApiAsync(
                            new Dictionary <string, object>()
                    {
                        { "command", "APPEND" },
                        { "media_id", mediaId },
                        { "segment_index", segmentIndex },
                        { "media", chunk }
                    }, cancellationToken
                            )
                        .Done(res => res.Dispose(), cancellationToken)
                        );
                }

                return WhenAll(tasks)
                .Done(() =>
                {
                    return AccessUploadApiAsync(
                        new Dictionary <string, object>()
                    {
                        { "command", "FINALIZE" },
                        { "media_id", mediaId }
                    }, cancellationToken)
                    .ReadResponse(s => CoreBase.Convert <MediaUploadResult>(s), cancellationToken);
                }, cancellationToken
                      )
                .Unwrap();
            }, cancellationToken)
                   .Unwrap());
        }
예제 #10
0
 /// <summary>
 /// Uploads videos or chunked images to Twitter for use in a Tweet or Twitter-hosted Card as an asynchronous operation.
 /// </summary>
 /// <param name="media">The raw binary file content being uploaded.</param>
 /// <param name="mediaType">The type of the media being uploaded.</param>
 /// <param name="additional_owners">A comma-separated string of user IDs to set as additional owners who are allowed to use the returned media_id in Tweets or Cards.</param>
 /// <param name="cancellationToken">The cancellation token.</param>
 /// <returns>The result for the uploaded media.</returns>
 public Task<MediaUploadResult> UploadChunkedAsync(Stream media, UploadMediaType mediaType, IEnumerable<long> additional_owners = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     return this.UploadChunkedAsync(media, checked((int)media.Length), mediaType, additional_owners, cancellationToken);
 }
예제 #11
0
        /// <summary>
        /// 获取素材列表
        /// </summary>
        /// <param name="accessToken">调用接口凭证</param>
        /// <param name="agentid">企业应用的id,整型。可在应用的设置页面查看</param>
        /// <param name="mediaType">素材类型</param>
        /// <param name="offset">从该类型素材的该偏移位置开始返回,0表示从第一个素材 返回</param>
        /// <param name="count">返回素材的数量,取值在1到50之间</param>
        public MaterialGetBatch GetMaterialBatch(string accessToken, int agentid, UploadMediaType mediaType, int offset, int count)
        {
            MaterialGetBatch materialBatch = new MaterialGetBatch();
            var data = new { type = mediaType.ToString(), agentid = agentid, offset = offset, count = count };
            var dataJson = JsonConvert.SerializeObject(data);
            var resultJson = HttpHelper.PostString(EntRequestUrl.GetMaterialBatch.ToUrl(accessToken), dataJson);

            WeChatResult result = JsonConvert.DeserializeObject<WeChatResult>(resultJson);
            if (!result.IsSuccess)
            {
                return materialBatch;
            }
            JObject dataObj = (JObject)JsonConvert.DeserializeObject(resultJson);
            var type = dataObj["type"].ToString();
            UploadMediaType mt = (UploadMediaType)Enum.Parse(typeof(UploadMediaType), type);

            if (mt == UploadMediaType.mpnews)
            {
                materialBatch.MediaComplexList = new List<MediaComplex>();
                foreach (var item in dataObj["itemlist"])
                {
                    string value = item.ToString();
                    MediaComplex ma = JsonConvert.DeserializeObject<MediaComplex>(value);
                    ma.articles = new List<Material>();

                    JObject articles = (JObject)JsonConvert.DeserializeObject(value);
                    foreach (var article in articles["content"]["articles"])
                    {
                        ma.articles.Add(JsonConvert.DeserializeObject<Material>(article.ToString()));
                    }
                    materialBatch.MediaComplexList.Add(ma);
                }
            }
            else
            {
                materialBatch.MediaSimpleList = new List<MediaSimple>();
                foreach (var item in dataObj["itemlist"])
                {
                    string value = item.ToString();
                    MediaSimple ma = JsonConvert.DeserializeObject<MediaSimple>(value);
                    materialBatch.MediaSimpleList.Add(ma);
                }
            }
            materialBatch.TotalCount = int.Parse(dataObj["total_count"].ToString());
            materialBatch.ItemCount = int.Parse(dataObj["item_count"].ToString());
            return materialBatch;
        }
예제 #12
0
파일: Media.cs 프로젝트: kyanro/CoreTweet
 /// <summary>
 /// Uploads videos or chunked images to Twitter for use in a Tweet or Twitter-hosted Card.
 /// </summary>
 /// <param name="media">The raw binary file content being uploaded.</param>
 /// <param name="mediaType">The type of the media being uploaded.</param>
 /// <param name="additional_owners">A comma-separated string of user IDs to set as additional owners who are allowed to use the returned media_id in Tweets or Cards.</param>
 /// <returns>The result for the uploaded media.</returns>
 public MediaUploadResult UploadChunked(Stream media, UploadMediaType mediaType, IEnumerable<long> additional_owners = null)
 {
     return this.UploadChunked(media, checked((int)media.Length), mediaType, additional_owners);
 }
예제 #13
0
파일: Media.cs 프로젝트: kyanro/CoreTweet
 /// <summary>
 /// <para>Uploads videos or chunked images to Twitter for use in a Tweet or Twitter-hosted Card.</para>
 /// <para>Available parameters:</para>
 /// <para>- <c>IEnumerbale&lt;long&gt;</c> additional_owners (optional)</para>
 /// </summary>
 /// <param name="media">The raw binary file content being uploaded.</param>
 /// <param name="totalBytes">The size of the media being uploaded in bytes.</param>
 /// <param name="mediaType">The type of the media being uploaded.</param>
 /// <param name="parameters">The parameters.</param>
 /// <returns>The result for the uploaded media.</returns>
 public MediaUploadResult UploadChunked(Stream media, int totalBytes, UploadMediaType mediaType, object parameters)
 {
     return this.UploadChunkedImpl(media, totalBytes, mediaType, InternalUtils.ResolveObject(parameters));
 }
예제 #14
0
 /// <summary>
 /// <para>Uploads videos or chunked images to Twitter for use in a Tweet or Twitter-hosted Card.</para>
 /// <para>Available parameters:</para>
 /// <para>- <c>string</c> media_category (optional)</para>
 /// <para>- <c>IEnumerbale&lt;long&gt;</c> additional_owners (optional)</para>
 /// </summary>
 /// <param name="media">The raw binary file content being uploaded.</param>
 /// <param name="mediaType">The type of the media being uploaded.</param>
 /// <param name="parameters">The parameters.</param>
 /// <returns>The result for the uploaded media.</returns>
 public MediaUploadResult UploadChunked(Stream media, UploadMediaType mediaType, params Expression <Func <string, object> >[] parameters)
 {
     return(this.UploadChunked(media, media.Length, mediaType, parameters));
 }
예제 #15
0
 /// <summary>
 /// Uploads videos or chunked images to Twitter for use in a Tweet or Twitter-hosted Card as an asynchronous operation.
 /// </summary>
 /// <param name="media">The raw binary file content being uploaded.</param>
 /// <param name="mediaType">The type of the media being uploaded.</param>
 /// <param name="media_category">A string enum value which identifies a media usecase.</param>
 /// <param name="additional_owners">A comma-separated string of user IDs to set as additional owners who are allowed to use the returned media_id in Tweets or Cards.</param>
 /// <param name="cancellationToken">The cancellation token.</param>
 /// <returns>The result for the uploaded media.</returns>
 public Task <MediaUploadResult> UploadChunkedWithRetryAsync(Stream media, UploadMediaType mediaType, int retryCount, int retryDelayInMilliseconds, string media_category = null, IEnumerable <long> additional_owners = null, CancellationToken cancellationToken = default(CancellationToken), IProgress <UploadChunkedProgressInfo> progress = null)
 {
     return(this.UploadChunkedWithRetryAsync(media, media.Length, mediaType, retryCount, retryDelayInMilliseconds, media_category, additional_owners, cancellationToken, progress));
 }
예제 #16
0
 /// <summary>
 /// <para>Uploads videos or chunked images to Twitter for use in a Tweet or Twitter-hosted Card as an asynchronous operation.</para>
 /// <para>Available parameters:</para>
 /// <para>- <c>string</c> media_category (optional)</para>
 /// <para>- <c>IEnumerbale&lt;long&gt;</c> additional_owners (optional)</para>
 /// </summary>
 /// <param name="media">The raw binary file content being uploaded.</param>
 /// <param name="mediaType">The type of the media being uploaded.</param>
 /// <param name="parameters">The parameters.</param>
 /// <param name="cancellationToken">The cancellation token.</param>
 /// <returns>
 /// <para>The task object representing the asynchronous operation.</para>
 /// <para>The Result property on the task object returns the result for the uploaded media.</para>
 /// </returns>
 public Task <MediaUploadResult> UploadChunkedWithRetryAsync(Stream media, UploadMediaType mediaType, object parameters, int retryCount, int retryDelayInMilliseconds, CancellationToken cancellationToken = default(CancellationToken), IProgress <UploadChunkedProgressInfo> progress = null)
 {
     return(this.UploadChunkedWithRetryAsync(media, media.Length, mediaType, retryCount, retryDelayInMilliseconds, parameters, cancellationToken, progress));
 }
예제 #17
0
        /// <summary>
        /// Uploads videos or chunked images to Twitter for use in a Tweet or Twitter-hosted Card as an asynchronous operation.
        /// </summary>
        /// <param name="media">The raw binary file content being uploaded.</param>
        /// <param name="totalBytes">The size of the media being uploaded in bytes.</param>
        /// <param name="mediaType">The type of the media being uploaded.</param>
        /// <param name="media_category">A string enum value which identifies a media usecase.</param>
        /// <param name="additional_owners">A comma-separated string of user IDs to set as additional owners who are allowed to use the returned media_id in Tweets or Cards.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>The result for the uploaded media.</returns>
        public Task <MediaUploadResult> UploadChunkedWithRetryAsync(Stream media, long totalBytes, UploadMediaType mediaType, int retryCount, int retryDelayInMilliseconds, string media_category = null, IEnumerable <long> additional_owners = null, CancellationToken cancellationToken = default(CancellationToken), IProgress <UploadChunkedProgressInfo> progress = null)
        {
            var parameters = new Dictionary <string, object>();

            if (media_category != null)
            {
                parameters.Add(nameof(media_category), media_category);
            }
            if (additional_owners != null)
            {
                parameters.Add(nameof(additional_owners), additional_owners);
            }
            return(this.UploadChunkedAsyncImpl(media, totalBytes, mediaType, parameters, retryCount, retryDelayInMilliseconds, cancellationToken, progress));
        }
예제 #18
0
 /// <summary>
 /// <para>Uploads videos or chunked images to Twitter for use in a Tweet or Twitter-hosted Card as an asynchronous operation.</para>
 /// <para>Available parameters:</para>
 /// <para>- <c>IEnumerbale&lt;long&gt;</c> additional_owners (optional)</para>
 /// </summary>
 /// <param name="media">The raw binary file content being uploaded.</param>
 /// <param name="mediaType">The type of the media being uploaded.</param>
 /// <param name="parameters">The parameters.</param>
 /// <param name="cancellationToken">The cancellation token.</param>
 /// <returns>
 /// <para>The task object representing the asynchronous operation.</para>
 /// <para>The Result property on the task object returns the result for the uploaded media.</para>
 /// </returns>
 public Task <MediaUploadResult> UploadChunkedAsync(Stream media, UploadMediaType mediaType, object parameters, CancellationToken cancellationToken = default(CancellationToken))
 {
     return(this.UploadChunkedAsync(media, checked ((int)media.Length), mediaType, parameters, cancellationToken));
 }
예제 #19
0
 /// <summary>
 /// <para>Uploads videos or chunked images to Twitter for use in a Tweet or Twitter-hosted Card.</para>
 /// <para>Available parameters:</para>
 /// <para>- <c>string</c> media_category (optional)</para>
 /// <para>- <c>IEnumerbale&lt;long&gt;</c> additional_owners (optional)</para>
 /// </summary>
 /// <param name="media">The raw binary file content being uploaded.</param>
 /// <param name="mediaType">The type of the media being uploaded.</param>
 /// <param name="parameters">The parameters.</param>
 /// <returns>The result for the uploaded media.</returns>
 public MediaUploadResult UploadChunked(Stream media, UploadMediaType mediaType, IDictionary <string, object> parameters)
 {
     return(this.UploadChunked(media, media.Length, mediaType, parameters));
 }
예제 #20
0
파일: Media.cs 프로젝트: kyanro/CoreTweet
 /// <summary>
 /// <para>Uploads videos or chunked images to Twitter for use in a Tweet or Twitter-hosted Card.</para>
 /// <para>Available parameters:</para>
 /// <para>- <c>IEnumerbale&lt;long&gt;</c> additional_owners (optional)</para>
 /// </summary>
 /// <param name="media">The raw binary file content being uploaded.</param>
 /// <param name="totalBytes">The size of the media being uploaded in bytes.</param>
 /// <param name="mediaType">The type of the media being uploaded.</param>
 /// <param name="parameters">The parameters.</param>
 /// <returns>The result for the uploaded media.</returns>
 public MediaUploadResult UploadChunked(Stream media, int totalBytes, UploadMediaType mediaType, params Expression<Func<string, object>>[] parameters)
 {
     return this.UploadChunkedImpl(media, totalBytes, mediaType, InternalUtils.ExpressionsToDictionary(parameters));
 }
예제 #21
0
 /// <summary>
 /// Uploads videos or chunked images to Twitter for use in a Tweet or Twitter-hosted Card.
 /// </summary>
 /// <param name="media">The raw binary file content being uploaded.</param>
 /// <param name="mediaType">The type of the media being uploaded.</param>
 /// <param name="media_category">A string enum value which identifies a media usecase.</param>
 /// <param name="additional_owners">A comma-separated string of user IDs to set as additional owners who are allowed to use the returned media_id in Tweets or Cards.</param>
 /// <returns>The result for the uploaded media.</returns>
 public MediaUploadResult UploadChunked(Stream media, UploadMediaType mediaType, string media_category = null, IEnumerable <long> additional_owners = null)
 {
     return(this.UploadChunked(media, media.Length, mediaType, media_category, additional_owners));
 }
예제 #22
0
파일: Media.cs 프로젝트: kyanro/CoreTweet
 /// <summary>
 /// <para>Uploads videos or chunked images to Twitter for use in a Tweet or Twitter-hosted Card.</para>
 /// <para>Available parameters:</para>
 /// <para>- <c>IEnumerbale&lt;long&gt;</c> additional_owners (optional)</para>
 /// </summary>
 /// <param name="media">The raw binary file content being uploaded.</param>
 /// <param name="mediaType">The type of the media being uploaded.</param>
 /// <param name="parameters">The parameters.</param>
 /// <returns>The result for the uploaded media.</returns>
 public MediaUploadResult UploadChunked(Stream media, UploadMediaType mediaType, params Expression<Func<string, object>>[] parameters)
 {
     return this.UploadChunked(media, checked((int)media.Length), mediaType, parameters);
 }
예제 #23
0
 /// <summary>
 /// <para>Uploads videos or chunked images to Twitter for use in a Tweet or Twitter-hosted Card as an asynchronous operation.</para>
 /// <para>Available parameters:</para>
 /// <para>- <c>string</c> media_category (optional)</para>
 /// <para>- <c>IEnumerbale&lt;long&gt;</c> additional_owners (optional)</para>
 /// </summary>
 /// <param name="media">The raw binary file content being uploaded.</param>
 /// <param name="totalBytes">The size of the media being uploaded in bytes.</param>
 /// <param name="mediaType">The type of the media being uploaded.</param>
 /// <param name="parameters">The parameters.</param>
 /// <returns>
 /// <para>The task object representing the asynchronous operation.</para>
 /// <para>The Result property on the task object returns the result for the uploaded media.</para>
 /// </returns>
 public Task <MediaUploadResult> UploadChunkedWithRetryAsync(Stream media, long totalBytes, UploadMediaType mediaType, int retryCount, int retryDelayInMilliseconds, params Expression <Func <string, object> >[] parameters)
 {
     return(this.UploadChunkedAsyncImpl(media, totalBytes, mediaType, InternalUtils.ExpressionsToDictionary(parameters), retryCount, retryDelayInMilliseconds, CancellationToken.None));
 }
예제 #24
0
파일: Media.cs 프로젝트: kyanro/CoreTweet
 private static string GetMediaTypeString(UploadMediaType mediaType)
 {
     return mediaType == UploadMediaType.Video ? "video/mp4" : "application/octet-stream";
 }
예제 #25
0
파일: Media.cs 프로젝트: cannorin/CoreTweet
 /// <summary>
 /// <para>Uploads videos or chunked images to Twitter for use in a Tweet or Twitter-hosted Card.</para>
 /// <para>Available parameters:</para>
 /// <para>- <c>IEnumerbale&lt;long&gt;</c> additional_owners (optional)</para>
 /// </summary>
 /// <param name="media">The raw binary file content being uploaded.</param>
 /// <param name="totalBytes">The size of the media being uploaded in bytes.</param>
 /// <param name="mediaType">The type of the media being uploaded.</param>
 /// <param name="parameters">The parameters.</param>
 /// <returns>The result for the uploaded media.</returns>
 public MediaUploadResult UploadChunked(Stream media, int totalBytes, UploadMediaType mediaType, params Expression <Func <string, object> >[] parameters)
 {
     return(this.UploadChunkedImpl(media, totalBytes, mediaType, InternalUtils.ExpressionsToDictionary(parameters)));
 }
예제 #26
0
 /// <summary>
 /// <para>Uploads videos or chunked images to Twitter for use in a Tweet or Twitter-hosted Card as an asynchronous operation.</para>
 /// <para>Available parameters:</para>
 /// <para>- <c>string</c> media_category (optional)</para>
 /// <para>- <c>IEnumerbale&lt;long&gt;</c> additional_owners (optional)</para>
 /// </summary>
 /// <param name="media">The raw binary file content being uploaded.</param>
 /// <param name="totalBytes">The size of the media being uploaded in bytes.</param>
 /// <param name="mediaType">The type of the media being uploaded.</param>
 /// <param name="parameters">The parameters.</param>
 /// <param name="cancellationToken">The cancellation token.</param>
 /// <returns>
 /// <para>The task object representing the asynchronous operation.</para>
 /// <para>The Result property on the task object returns the result for the uploaded media.</para>
 /// </returns>
 public Task <MediaUploadResult> UploadChunkedWithRetryAsync(Stream media, long totalBytes, UploadMediaType mediaType, int retryCount, int retryDelayInMilliseconds, object parameters, CancellationToken cancellationToken = default(CancellationToken), IProgress <UploadChunkedProgressInfo> progress = null)
 {
     return(this.UploadChunkedAsyncImpl(media, totalBytes, mediaType, InternalUtils.ResolveObject(parameters), retryCount, retryDelayInMilliseconds, cancellationToken, progress));
 }
예제 #27
0
파일: Media.cs 프로젝트: cannorin/CoreTweet
 /// <summary>
 /// <para>Uploads videos or chunked images to Twitter for use in a Tweet or Twitter-hosted Card.</para>
 /// <para>Available parameters:</para>
 /// <para>- <c>IEnumerbale&lt;long&gt;</c> additional_owners (optional)</para>
 /// </summary>
 /// <param name="media">The raw binary file content being uploaded.</param>
 /// <param name="totalBytes">The size of the media being uploaded in bytes.</param>
 /// <param name="mediaType">The type of the media being uploaded.</param>
 /// <param name="parameters">The parameters.</param>
 /// <returns>The result for the uploaded media.</returns>
 public MediaUploadResult UploadChunked(Stream media, int totalBytes, UploadMediaType mediaType, IDictionary <string, object> parameters)
 {
     return(this.UploadChunkedImpl(media, totalBytes, mediaType, parameters));
 }
예제 #28
0
        private Task<MediaUploadResult> UploadChunkedAsyncImpl(Stream media, int totalBytes, UploadMediaType mediaType, IEnumerable<KeyValuePair<string, object>> parameters, CancellationToken cancellationToken)
        {
            return this.AccessUploadApiAsync(
                new Dictionary<string, object>()
                {
                    { "command", "INIT" },
                    { "total_bytes", totalBytes },
                    { "media_type", GetMediaTypeString(mediaType) }
                }.Concat(parameters), cancellationToken)
                .ContinueWith(
                    t => InternalUtils.ReadResponse(t, s => (string)JObject.Parse(s)["media_id_string"], cancellationToken),
                    cancellationToken
                )
                .Unwrap()
                .ContinueWith(t =>
                {
                    if(t.IsFaulted)
                        t.Exception.InnerException.Rethrow();

                    var mediaId = t.Result;
                    var tasks = new List<Task>();
                    const int maxChunkSize = 5 * 1000 * 1000;
                    var remainingBytes = totalBytes;

                    for (var i = 0; remainingBytes > 0; i++)
                    {
                        var segmentIndex = i; // to capture the variable
                        var chunkSize = remainingBytes < maxChunkSize ? remainingBytes : maxChunkSize;
                        var chunk = new byte[chunkSize];
                        var readCount = media.Read(chunk, 0, chunkSize);
                        if (readCount == 0) break;
                        if (chunkSize != readCount)
                        {
                            var newChunk = new byte[readCount];
                            Buffer.BlockCopy(chunk, 0, newChunk, 0, readCount);
                            chunk = newChunk;
                        }
                        remainingBytes -= readCount;
                        tasks.Add(
                            this.AccessUploadApiAsync(
                                new Dictionary<string, object>()
                                {
                                    { "command", "APPEND" },
                                    { "media_id", mediaId },
                                    { "segment_index", segmentIndex },
                                    { "media", chunk }
                                }, cancellationToken
                            )
                            .ContinueWith(t2 =>
                            {
                                if (t2.IsFaulted)
                                    t2.Exception.InnerException.Rethrow();

                                t2.Result.Dispose();
                            }, cancellationToken)
                        );
                    }

                    return WhenAll(tasks)
                        .ContinueWith(x =>
                        {
                            if(x.IsFaulted)
                                x.Exception.InnerException.Rethrow();

                            return AccessUploadApiAsync(
                                new Dictionary<string, object>()
                                {
                                    { "command", "FINALIZE" },
                                    { "media_id", mediaId }
                                }, cancellationToken)
                                .ContinueWith(
                                    y => InternalUtils.ReadResponse(y, s => CoreBase.Convert<MediaUploadResult>(s), cancellationToken),
                                    cancellationToken
                                )
                                .Unwrap();
                        }, cancellationToken
                        )
                        .Unwrap();
                }, cancellationToken)
                .Unwrap();
        }
예제 #29
0
파일: Media.cs 프로젝트: cannorin/CoreTweet
 /// <summary>
 /// <para>Uploads videos or chunked images to Twitter for use in a Tweet or Twitter-hosted Card.</para>
 /// <para>Available parameters:</para>
 /// <para>- <c>IEnumerbale&lt;long&gt;</c> additional_owners (optional)</para>
 /// </summary>
 /// <param name="media">The raw binary file content being uploaded.</param>
 /// <param name="totalBytes">The size of the media being uploaded in bytes.</param>
 /// <param name="mediaType">The type of the media being uploaded.</param>
 /// <param name="parameters">The parameters.</param>
 /// <returns>The result for the uploaded media.</returns>
 public MediaUploadResult UploadChunked(Stream media, int totalBytes, UploadMediaType mediaType, object parameters)
 {
     return(this.UploadChunkedImpl(media, totalBytes, mediaType, InternalUtils.ResolveObject(parameters)));
 }
예제 #30
0
 /// <summary>
 /// <para>Uploads videos or chunked images to Twitter for use in a Tweet or Twitter-hosted Card as an asynchronous operation.</para>
 /// <para>Available parameters:</para>
 /// <para>- <c>IEnumerbale&lt;long&gt;</c> additional_owners (optional)</para>
 /// </summary>
 /// <param name="media">The raw binary file content being uploaded.</param>
 /// <param name="totalBytes">The size of the media being uploaded in bytes.</param>
 /// <param name="mediaType">The type of the media being uploaded.</param>
 /// <param name="parameters">The parameters.</param>
 /// <returns>
 /// <para>The task object representing the asynchronous operation.</para>
 /// <para>The Result property on the task object returns the result for the uploaded media.</para>
 /// </returns>
 public Task <MediaUploadResult> UploadChunkedAsync(Stream media, int totalBytes, UploadMediaType mediaType, params Expression <Func <string, object> >[] parameters)
 {
     return(this.UploadChunkedAsyncImpl(media, totalBytes, mediaType, InternalUtils.ExpressionsToDictionary(parameters), CancellationToken.None));
 }
예제 #31
0
 /// <summary>
 /// <para>Uploads videos or chunked images to Twitter for use in a Tweet or Twitter-hosted Card as an asynchronous operation.</para>
 /// <para>Available parameters:</para>
 /// <para>- <c>string</c> media_category (optional)</para>
 /// <para>- <c>IEnumerbale&lt;long&gt;</c> additional_owners (optional)</para>
 /// </summary>
 /// <param name="media">The raw binary file content being uploaded.</param>
 /// <param name="mediaType">The type of the media being uploaded.</param>
 /// <param name="parameters">The parameters.</param>
 /// <param name="cancellationToken">The cancellation token.</param>
 /// <returns>
 /// <para>The task object representing the asynchronous operation.</para>
 /// <para>The Result property on the task object returns the result for the uploaded media.</para>
 /// </returns>
 public Task <MediaUploadResult> UploadChunkedAsync(Stream media, UploadMediaType mediaType, object parameters, CancellationToken cancellationToken = default(CancellationToken), IProgress <UploadChunkedProgressInfo> progress = null)
 {
     return(this.UploadChunkedAsync(media, media.Length, mediaType, parameters, cancellationToken, progress));
 }
예제 #32
0
 /// <summary>
 /// <para>Uploads videos or chunked images to Twitter for use in a Tweet or Twitter-hosted Card as an asynchronous operation.</para>
 /// <para>Available parameters:</para>
 /// <para>- <c>IEnumerbale&lt;long&gt;</c> additional_owners (optional)</para>
 /// </summary>
 /// <param name="media">The raw binary file content being uploaded.</param>
 /// <param name="totalBytes">The size of the media being uploaded in bytes.</param>
 /// <param name="mediaType">The type of the media being uploaded.</param>
 /// <param name="parameters">The parameters.</param>
 /// <param name="cancellationToken">The cancellation token.</param>
 /// <returns>
 /// <para>The task object representing the asynchronous operation.</para>
 /// <para>The Result property on the task object returns the result for the uploaded media.</para>
 /// </returns>
 public Task <MediaUploadResult> UploadChunkedAsync(Stream media, int totalBytes, UploadMediaType mediaType, object parameters, CancellationToken cancellationToken = default(CancellationToken))
 {
     return(this.UploadChunkedAsyncImpl(media, totalBytes, mediaType, InternalUtils.ResolveObject(parameters), cancellationToken));
 }
예제 #33
0
파일: Media.cs 프로젝트: cannorin/CoreTweet
 /// <summary>
 /// <para>Uploads videos or chunked images to Twitter for use in a Tweet or Twitter-hosted Card.</para>
 /// <para>Available parameters:</para>
 /// <para>- <c>IEnumerbale&lt;long&gt;</c> additional_owners (optional)</para>
 /// </summary>
 /// <param name="media">The raw binary file content being uploaded.</param>
 /// <param name="mediaType">The type of the media being uploaded.</param>
 /// <param name="parameters">The parameters.</param>
 /// <returns>The result for the uploaded media.</returns>
 public MediaUploadResult UploadChunked(Stream media, UploadMediaType mediaType, object parameters)
 {
     return(this.UploadChunked(media, checked ((int)media.Length), mediaType, parameters));
 }
예제 #34
0
 /// <summary>
 /// <para>Uploads videos or chunked images to Twitter for use in a Tweet or Twitter-hosted Card as an asynchronous operation.</para>
 /// <para>Available parameters:</para>
 /// <para>- <c>IEnumerbale&lt;long&gt;</c> additional_owners (optional)</para>
 /// </summary>
 /// <param name="media">The raw binary file content being uploaded.</param>
 /// <param name="mediaType">The type of the media being uploaded.</param>
 /// <param name="parameters">The parameters.</param>
 /// <returns>
 /// <para>The task object representing the asynchronous operation.</para>
 /// <para>The Result property on the task object returns the result for the uploaded media.</para>
 /// </returns>
 public Task <MediaUploadResult> UploadChunkedAsync(Stream media, UploadMediaType mediaType, params Expression <Func <string, object> >[] parameters)
 {
     return(this.UploadChunkedAsync(media, checked ((int)media.Length), mediaType, parameters));
 }
예제 #35
0
파일: Media.cs 프로젝트: cannorin/CoreTweet
 /// <summary>
 /// Uploads videos or chunked images to Twitter for use in a Tweet or Twitter-hosted Card.
 /// </summary>
 /// <param name="media">The raw binary file content being uploaded.</param>
 /// <param name="mediaType">The type of the media being uploaded.</param>
 /// <param name="additional_owners">A comma-separated string of user IDs to set as additional owners who are allowed to use the returned media_id in Tweets or Cards.</param>
 /// <returns>The result for the uploaded media.</returns>
 public MediaUploadResult UploadChunked(Stream media, UploadMediaType mediaType, IEnumerable <long> additional_owners = null)
 {
     return(this.UploadChunked(media, checked ((int)media.Length), mediaType, additional_owners));
 }
예제 #36
0
 /// <summary>
 /// Uploads videos or chunked images to Twitter for use in a Tweet or Twitter-hosted Card as an asynchronous operation.
 /// </summary>
 /// <param name="media">The raw binary file content being uploaded.</param>
 /// <param name="mediaType">The type of the media being uploaded.</param>
 /// <param name="additional_owners">A comma-separated string of user IDs to set as additional owners who are allowed to use the returned media_id in Tweets or Cards.</param>
 /// <param name="cancellationToken">The cancellation token.</param>
 /// <returns>The result for the uploaded media.</returns>
 public Task <MediaUploadResult> UploadChunkedAsync(Stream media, UploadMediaType mediaType, IEnumerable <long> additional_owners = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     return(this.UploadChunkedAsync(media, checked ((int)media.Length), mediaType, additional_owners, cancellationToken));
 }
예제 #37
0
파일: Media.cs 프로젝트: cannorin/CoreTweet
 private static string GetMediaTypeString(UploadMediaType mediaType)
 {
     return(mediaType == UploadMediaType.Video ? "video/mp4" : "application/octet-stream");
 }
예제 #38
0
파일: Media.cs 프로젝트: kyanro/CoreTweet
 /// <summary>
 /// <para>Uploads videos or chunked images to Twitter for use in a Tweet or Twitter-hosted Card.</para>
 /// <para>Available parameters:</para>
 /// <para>- <c>IEnumerbale&lt;long&gt;</c> additional_owners (optional)</para>
 /// </summary>
 /// <param name="media">The raw binary file content being uploaded.</param>
 /// <param name="totalBytes">The size of the media being uploaded in bytes.</param>
 /// <param name="mediaType">The type of the media being uploaded.</param>
 /// <param name="parameters">The parameters.</param>
 /// <returns>The result for the uploaded media.</returns>
 public MediaUploadResult UploadChunked(Stream media, int totalBytes, UploadMediaType mediaType, IDictionary<string, object> parameters)
 {
     return this.UploadChunkedImpl(media, totalBytes, mediaType, parameters);
 }
예제 #39
0
 /// <summary>
 /// <para>Uploads videos or chunked images to Twitter for use in a Tweet or Twitter-hosted Card as an asynchronous operation.</para>
 /// <para>Available parameters:</para>
 /// <para>- <c>IEnumerbale&lt;long&gt;</c> additional_owners (optional)</para>
 /// </summary>
 /// <param name="media">The raw binary file content being uploaded.</param>
 /// <param name="totalBytes">The size of the media being uploaded in bytes.</param>
 /// <param name="mediaType">The type of the media being uploaded.</param>
 /// <param name="parameters">The parameters.</param>
 /// <returns>
 /// <para>The task object representing the asynchronous operation.</para>
 /// <para>The Result property on the task object returns the result for the uploaded media.</para>
 /// </returns>
 public Task<MediaUploadResult> UploadChunkedAsync(Stream media, int totalBytes, UploadMediaType mediaType, params Expression<Func<string, object>>[] parameters)
 {
     return this.UploadChunkedAsyncImpl(media, totalBytes, mediaType, InternalUtils.ExpressionsToDictionary(parameters), CancellationToken.None);
 }
예제 #40
0
파일: Media.cs 프로젝트: kyanro/CoreTweet
 /// <summary>
 /// Uploads videos or chunked images to Twitter for use in a Tweet or Twitter-hosted Card.
 /// </summary>
 /// <param name="media">The raw binary file content being uploaded.</param>
 /// <param name="totalBytes">The size of the media being uploaded in bytes.</param>
 /// <param name="mediaType">The type of the media being uploaded.</param>
 /// <param name="additional_owners">A comma-separated string of user IDs to set as additional owners who are allowed to use the returned media_id in Tweets or Cards.</param>
 /// <returns>The result for the uploaded media.</returns>
 public MediaUploadResult UploadChunked(Stream media, int totalBytes, UploadMediaType mediaType, IEnumerable<long> additional_owners = null)
 {
     var parameters = new Dictionary<string, object>();
     if (additional_owners != null) parameters.Add(nameof(additional_owners), additional_owners);
     return this.UploadChunkedImpl(media, totalBytes, mediaType, parameters);
 }
예제 #41
0
 /// <summary>
 /// <para>Uploads videos or chunked images to Twitter for use in a Tweet or Twitter-hosted Card as an asynchronous operation.</para>
 /// <para>Available parameters:</para>
 /// <para>- <c>IEnumerbale&lt;long&gt;</c> additional_owners (optional)</para>
 /// </summary>
 /// <param name="media">The raw binary file content being uploaded.</param>
 /// <param name="totalBytes">The size of the media being uploaded in bytes.</param>
 /// <param name="mediaType">The type of the media being uploaded.</param>
 /// <param name="parameters">The parameters.</param>
 /// <param name="cancellationToken">The cancellation token.</param>
 /// <returns>
 /// <para>The task object representing the asynchronous operation.</para>
 /// <para>The Result property on the task object returns the result for the uploaded media.</para>
 /// </returns>
 public Task<MediaUploadResult> UploadChunkedAsync(Stream media, int totalBytes, UploadMediaType mediaType, IDictionary<string, object> parameters, CancellationToken cancellationToken = default(CancellationToken))
 {
     return this.UploadChunkedAsyncImpl(media, totalBytes, mediaType, parameters, cancellationToken);
 }
예제 #42
0
파일: Media.cs 프로젝트: kyanro/CoreTweet
 /// <summary>
 /// <para>Uploads videos or chunked images to Twitter for use in a Tweet or Twitter-hosted Card.</para>
 /// <para>Available parameters:</para>
 /// <para>- <c>IEnumerbale&lt;long&gt;</c> additional_owners (optional)</para>
 /// </summary>
 /// <param name="media">The raw binary file content being uploaded.</param>
 /// <param name="mediaType">The type of the media being uploaded.</param>
 /// <param name="parameters">The parameters.</param>
 /// <returns>The result for the uploaded media.</returns>
 public MediaUploadResult UploadChunked(Stream media, UploadMediaType mediaType, object parameters)
 {
     return this.UploadChunked(media, checked((int)media.Length), mediaType, parameters);
 }
예제 #43
0
 /// <summary>
 /// <para>Uploads videos or chunked images to Twitter for use in a Tweet or Twitter-hosted Card as an asynchronous operation.</para>
 /// <para>Available parameters:</para>
 /// <para>- <c>string</c> media_category (optional)</para>
 /// <para>- <c>IEnumerbale&lt;long&gt;</c> additional_owners (optional)</para>
 /// </summary>
 /// <param name="media">The raw binary file content being uploaded.</param>
 /// <param name="totalBytes">The size of the media being uploaded in bytes.</param>
 /// <param name="mediaType">The type of the media being uploaded.</param>
 /// <param name="parameters">The parameters.</param>
 /// <param name="cancellationToken">The cancellation token.</param>
 /// <returns>
 /// <para>The task object representing the asynchronous operation.</para>
 /// <para>The Result property on the task object returns the result for the uploaded media.</para>
 /// </returns>
 public Task <MediaUploadResult> UploadChunkedAsync(Stream media, long totalBytes, UploadMediaType mediaType, IDictionary <string, object> parameters, CancellationToken cancellationToken = default(CancellationToken), IProgress <UploadChunkedProgressInfo> progress = null)
 {
     return(this.UploadChunkedAsyncImpl(media, totalBytes, mediaType, parameters, 0, 0, cancellationToken, progress));
 }
예제 #44
0
파일: Media.cs 프로젝트: kyanro/CoreTweet
        private MediaUploadResult UploadChunkedImpl(Stream media, int totalBytes, UploadMediaType mediaType, IEnumerable<KeyValuePair<string, object>> parameters)
        {
            string mediaId;
            using(var res = AccessUploadApi(new Dictionary<string, object>()
            {
                { "command", "INIT" },
                { "total_bytes", totalBytes },
                { "media_type", GetMediaTypeString(mediaType) }
            }.Concat(parameters)))
            using(var sr = new StreamReader(res.GetResponseStream()))
                mediaId = (string)JObject.Parse(sr.ReadToEnd())["media_id_string"];

            const int maxChunkSize = 5 * 1000 * 1000;
            byte[] chunk = null;
            var remainingBytes = totalBytes;
            for(var segmentIndex = 0; remainingBytes > 0; segmentIndex++)
            {
                var chunkSize = remainingBytes < maxChunkSize ? remainingBytes : maxChunkSize;
                if(chunk == null || chunk.Length != chunkSize)
                    chunk = new byte[chunkSize];
                var readCount = media.Read(chunk, 0, chunkSize);
                if(readCount == 0) break;
                if(chunkSize != readCount)
                {
                    var newChunk = new byte[readCount];
                    Buffer.BlockCopy(chunk, 0, newChunk, 0, readCount);
                    chunk = newChunk;
                }
                this.AccessUploadApi(new Dictionary<string, object>()
                {
                    { "command", "APPEND" },
                    { "media_id", mediaId },
                    { "segment_index", segmentIndex },
                    { "media", chunk }
                }).Close();
                remainingBytes -= readCount;
            }

            using(var res = AccessUploadApi(new Dictionary<string, object>()
            {
                { "command", "FINALIZE" },
                { "media_id", mediaId }
            }))
            using(var sr = new StreamReader(res.GetResponseStream()))
            {
                var json = sr.ReadToEnd();
                var result = CoreBase.Convert<MediaUploadResult>(json);
                result.Json = json;
                return result;
            }
        }
예제 #45
0
 /// <summary>
 /// Uploads videos or chunked images to Twitter for use in a Tweet or Twitter-hosted Card as an asynchronous operation.
 /// </summary>
 /// <param name="media">The raw binary file content being uploaded.</param>
 /// <param name="totalBytes">The size of the media being uploaded in bytes.</param>
 /// <param name="mediaType">The type of the media being uploaded.</param>
 /// <param name="additional_owners">A comma-separated string of user IDs to set as additional owners who are allowed to use the returned media_id in Tweets or Cards.</param>
 /// <param name="cancellationToken">The cancellation token.</param>
 /// <returns>The result for the uploaded media.</returns>
 public Task<MediaUploadResult> UploadChunkedAsync(Stream media, int totalBytes, UploadMediaType mediaType, IEnumerable<long> additional_owners = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     var parameters = new Dictionary<string, object>();
     if (additional_owners != null) parameters.Add("additional_owners", additional_owners);
     return this.UploadChunkedAsyncImpl(media, totalBytes, mediaType, parameters, cancellationToken);
 }
예제 #46
0
 /// <summary>
 /// 上传其他类型永久素材
 /// 所有文件size必须大于5个字节
 /// 图片(image):2MB,支持JPG,PNG格式
 /// 语音(voice):2MB,播放长度不超过60s,支持AMR格式
 /// 视频(video):10MB,支持MP4格式
 /// 普通文件(file):20MB
 /// 整个企业图文消息素材和图片素材数目的上限为5000,其他类型为1000
 /// 超出素材数量限制返回错误码45028
 /// </summary>
 /// <param name="accessToken">调用接口凭证</param>
 /// <param name="agentid">企业应用的id,整型。可在应用的设置页面查看</param>
 /// <param name="fileFullName">文件名称(包含文件路径)</param>
 /// <param name="mediaType">媒体文件类型</param>
 /// <returns></returns>
 public MaterialUploadResult UploadOtherMaterial(string accessToken, int agentid, string fileFullName, UploadMediaType mediaType)
 {
     var fileDictionary = new Dictionary<string, string>();
     fileDictionary["media"] = fileFullName;
     string resultJson = HttpHelper.PostFileGetJson(EntRequestUrl.AddMaterial.ToUrl(accessToken, agentid, mediaType.ToString()), null, fileDictionary, null, null);
     MaterialUploadResult result = JsonConvert.DeserializeObject<MaterialUploadResult>(resultJson);
     return result;
 }
예제 #47
0
 /// <summary>
 /// <para>Uploads videos or chunked images to Twitter for use in a Tweet or Twitter-hosted Card as an asynchronous operation.</para>
 /// <para>Available parameters:</para>
 /// <para>- <c>IEnumerbale&lt;long&gt;</c> additional_owners (optional)</para>
 /// </summary>
 /// <param name="media">The raw binary file content being uploaded.</param>
 /// <param name="mediaType">The type of the media being uploaded.</param>
 /// <param name="parameters">The parameters.</param>
 /// <param name="cancellationToken">The cancellation token.</param>
 /// <returns>
 /// <para>The task object representing the asynchronous operation.</para>
 /// <para>The Result property on the task object returns the result for the uploaded media.</para>
 /// </returns>
 public Task<MediaUploadResult> UploadChunkedAsync(Stream media, UploadMediaType mediaType, object parameters, CancellationToken cancellationToken = default(CancellationToken))
 {
     return this.UploadChunkedAsync(media, checked((int)media.Length), mediaType, parameters, cancellationToken);
 }
예제 #48
0
 /// <summary>
 /// <para>Uploads videos or chunked images to Twitter for use in a Tweet or Twitter-hosted Card as an asynchronous operation.</para>
 /// <para>Available parameters:</para>
 /// <para>- <c>IEnumerbale&lt;long&gt;</c> additional_owners (optional)</para>
 /// </summary>
 /// <param name="media">The raw binary file content being uploaded.</param>
 /// <param name="totalBytes">The size of the media being uploaded in bytes.</param>
 /// <param name="mediaType">The type of the media being uploaded.</param>
 /// <param name="parameters">The parameters.</param>
 /// <param name="cancellationToken">The cancellation token.</param>
 /// <returns>
 /// <para>The task object representing the asynchronous operation.</para>
 /// <para>The Result property on the task object returns the result for the uploaded media.</para>
 /// </returns>
 public Task<MediaUploadResult> UploadChunkedAsync(Stream media, int totalBytes, UploadMediaType mediaType, object parameters, CancellationToken cancellationToken = default(CancellationToken))
 {
     return this.UploadChunkedAsyncImpl(media, totalBytes, mediaType, InternalUtils.ResolveObject(parameters), cancellationToken);
 }
예제 #49
0
 /// <summary>
 /// <para>Uploads videos or chunked images to Twitter for use in a Tweet or Twitter-hosted Card as an asynchronous operation.</para>
 /// <para>Available parameters:</para>
 /// <para>- <c>string</c> media_category (optional)</para>
 /// <para>- <c>IEnumerbale&lt;long&gt;</c> additional_owners (optional)</para>
 /// </summary>
 /// <param name="media">The raw binary file content being uploaded.</param>
 /// <param name="mediaType">The type of the media being uploaded.</param>
 /// <param name="parameters">The parameters.</param>
 /// <returns>
 /// <para>The task object representing the asynchronous operation.</para>
 /// <para>The Result property on the task object returns the result for the uploaded media.</para>
 /// </returns>
 public Task <MediaUploadResult> UploadChunkedWithRetryAsync(Stream media, UploadMediaType mediaType, int retryCount, int retryDelayInMilliseconds, params Expression <Func <string, object> >[] parameters)
 {
     return(this.UploadChunkedWithRetryAsync(media, media.Length, mediaType, retryCount, retryDelayInMilliseconds, parameters));
 }