예제 #1
0
        /// <summary>
        /// Initializes the upload by creating a persistent upload tracking ID.
        /// </summary>
        /// <param name="fileParams">Describes the file to be uploaded.</param>
        /// <param name="auth">Your SAM auth object, if not already set up.</param>
        /// <returns>An object with the uploaded media_id in it.</returns>
        public SamUpload StartUpload(SamStartUploadParams fileParams, SamAuth auth = null)
        {
            var body     = Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(fileParams));
            var url      = string.Format("{0}/upload.xml", UploadBaseUrl);
            var response = request(url, body, null, auth);

            return(Utils.FromXml <SamUpload>(response));
        }
예제 #2
0
        /// <summary>
        /// Provides a wrapper method for the following upload methods that automatically
        /// initiates an upload, splits a byte array into appropriately sized parts, and
        /// finishes the upload when all of the parts have been uploaded. NOTE: not optimized
        /// for very large files. It's recommended that you read large files in parts from
        /// disk and use the separate functions below.
        /// </summary>
        /// <param name="fileParams">An object with the file info.</param>
        /// <param name="auth">Your SAM auth object, if not already set up.</param>
        /// <returns>An upload ID string.</returns>
        public string UploadMedia(byte[] bytes, string mimetype, string name = null, SamAuth auth = null)
        {
            var fileParams = new SamStartUploadParams();

            fileParams.mimetype = mimetype;
            fileParams.size     = bytes.Length;
            fileParams.name     = name;

            double parts = (double)bytes.Length / (double)Constants.PART_SIZE;

            fileParams.parts = (int)Math.Ceiling(parts);

            var uploadResult = StartUpload(fileParams, auth);

            using (var stream = new MemoryStream(bytes))
            {
                var    partNumber = 1;
                byte[] part       = new byte[Constants.PART_SIZE];

                while (stream.Position < stream.Length)
                {
                    var bytesRead = stream.Read(part, 0, Constants.PART_SIZE);
                    if (bytesRead < Constants.PART_SIZE)
                    {
                        Array.Resize(ref part, bytesRead);
                    }

                    var uploadParameters = new SamAppendUploadParams();
                    uploadParameters.part = partNumber;
                    uploadParameters.body = part;

                    AppendUpload(uploadResult.media_id, uploadParameters, auth);

                    partNumber++;
                }
            }

            CompleteUpload(uploadResult.media_id, auth);

            return(uploadResult.media_id);
        }