Exemple #1
0
        public FileMetaData UploadFile(String path, Stream stream)
        {
            // start a session
            var ussa = new UploadSessionStartArg();

            var url = string.Format("{0}/files/upload_session/start", CONTENT_API_URL);
            var req = CreateRequest(url, "POST");

            req.Headers[API_ARG_HEADER] = JsonConvert.SerializeObject(ussa);
            req.ContentType             = "application/octet-stream";
            req.ContentLength           = Math.Min(DROPBOX_MAX_CHUNK_UPLOAD, stream.Length);
            req.Timeout = 200000;

            var areq = new AsyncHttpRequest(req);

            byte[] buffer = new byte[Utility.Utility.DEFAULT_BUFFER_SIZE];

            ulong globalBytesRead = 0;

            using (var rs = areq.GetRequestStream())
            {
                int bytesRead = 0;
                do
                {
                    bytesRead        = stream.Read(buffer, 0, (int)Utility.Utility.DEFAULT_BUFFER_SIZE);
                    globalBytesRead += (ulong)bytesRead;
                    rs.Write(buffer, 0, bytesRead);
                }while (bytesRead > 0 && globalBytesRead < DROPBOX_MAX_CHUNK_UPLOAD);
            }

            var ussr = ReadJSONResponse <UploadSessionStartResult>(areq); // pun intended

            // keep appending until finished
            // 1) read into buffer
            while (globalBytesRead < (ulong)stream.Length)
            {
                var remaining = (ulong)stream.Length - globalBytesRead;

                // start an append request
                var usaa = new UploadSessionAppendArg();
                usaa.cursor.session_id = ussr.session_id;
                usaa.cursor.offset     = globalBytesRead;
                usaa.close             = remaining < DROPBOX_MAX_CHUNK_UPLOAD;
                url = string.Format("{0}/files/upload_session/append_v2", CONTENT_API_URL);

                req = CreateRequest(url, "POST");
                req.Headers[API_ARG_HEADER] = JsonConvert.SerializeObject(usaa);
                req.ContentType             = "application/octet-stream";
                req.ContentLength           = Math.Min(DROPBOX_MAX_CHUNK_UPLOAD, (long)remaining);
                req.Timeout = 200000;

                areq = new AsyncHttpRequest(req);

                ulong bytesReadInRequest = 0;
                using (var rs = areq.GetRequestStream())
                {
                    int bytesRead = 0;
                    do
                    {
                        bytesRead           = stream.Read(buffer, 0, (int)Utility.Utility.DEFAULT_BUFFER_SIZE);
                        bytesReadInRequest += (ulong)bytesRead;
                        globalBytesRead    += (ulong)bytesRead;
                        rs.Write(buffer, 0, bytesRead);
                    }while (bytesRead > 0 && bytesReadInRequest < Math.Min(remaining, DROPBOX_MAX_CHUNK_UPLOAD));
                }

                using (var response = GetResponse(areq))
                    using (var sr = new StreamReader(response.GetResponseStream()))
                        sr.ReadToEnd();
            }

            // finish session and commit
            try
            {
                var usfa = new UploadSessionFinishArg();
                usfa.cursor.session_id = ussr.session_id;
                usfa.cursor.offset     = (ulong)globalBytesRead;
                usfa.commit.path       = path;

                url = string.Format("{0}/files/upload_session/finish", CONTENT_API_URL);
                req = CreateRequest(url, "POST");
                req.Headers[API_ARG_HEADER] = JsonConvert.SerializeObject(usfa);
                req.ContentType             = "application/octet-stream";
                req.Timeout = 200000;

                return(ReadJSONResponse <FileMetaData>(req));
            }
            catch (Exception ex)
            {
                handleDropboxException(ex);
                throw;
            }
        }
Exemple #2
0
        public async Task <FileMetaData> UploadFileAsync(String path, Stream stream, CancellationToken cancelToken)
        {
            // start a session
            var ussa = new UploadSessionStartArg();

            var chunksize = (int)Math.Min(DROPBOX_MAX_CHUNK_UPLOAD, stream.Length);

            var req = CreateRequest(WebApi.Dropbox.UploadSessionStartUrl(), "POST");

            req.Headers[API_ARG_HEADER] = JsonConvert.SerializeObject(ussa);
            req.ContentType             = "application/octet-stream";
            req.ContentLength           = chunksize;
            req.Timeout = 200000;

            var areq = new AsyncHttpRequest(req);

            byte[] buffer     = new byte[Utility.Utility.DEFAULT_BUFFER_SIZE];
            int    sizeToRead = Math.Min((int)Utility.Utility.DEFAULT_BUFFER_SIZE, chunksize);

            ulong globalBytesRead = 0;

            using (var rs = areq.GetRequestStream())
            {
                int bytesRead = 0;
                do
                {
                    bytesRead = await stream.ReadAsync(buffer, 0, sizeToRead, cancelToken).ConfigureAwait(false);

                    globalBytesRead += (ulong)bytesRead;
                    await rs.WriteAsync(buffer, 0, bytesRead, cancelToken).ConfigureAwait(false);
                }while (bytesRead > 0 && globalBytesRead < (ulong)chunksize);
            }

            var ussr = await ReadJSONResponseAsync <UploadSessionStartResult>(areq, cancelToken); // pun intended

            // keep appending until finished
            // 1) read into buffer
            while (globalBytesRead < (ulong)stream.Length)
            {
                var remaining = (ulong)stream.Length - globalBytesRead;

                // start an append request
                var usaa = new UploadSessionAppendArg();
                usaa.cursor.session_id = ussr.session_id;
                usaa.cursor.offset     = globalBytesRead;
                usaa.close             = remaining < DROPBOX_MAX_CHUNK_UPLOAD;

                chunksize = (int)Math.Min(DROPBOX_MAX_CHUNK_UPLOAD, (long)remaining);

                req = CreateRequest(WebApi.Dropbox.UploadSessionAppendUrl(), "POST");
                req.Headers[API_ARG_HEADER] = JsonConvert.SerializeObject(usaa);
                req.ContentType             = "application/octet-stream";
                req.ContentLength           = chunksize;
                req.Timeout = 200000;

                areq = new AsyncHttpRequest(req);

                int bytesReadInRequest = 0;
                sizeToRead = Math.Min(chunksize, (int)Utility.Utility.DEFAULT_BUFFER_SIZE);
                using (var rs = areq.GetRequestStream())
                {
                    int bytesRead = 0;
                    do
                    {
                        bytesRead = await stream.ReadAsync(buffer, 0, sizeToRead, cancelToken).ConfigureAwait(false);

                        bytesReadInRequest += bytesRead;
                        globalBytesRead    += (ulong)bytesRead;
                        await rs.WriteAsync(buffer, 0, bytesRead, cancelToken).ConfigureAwait(false);
                    }while (bytesRead > 0 && bytesReadInRequest < chunksize);
                }

                using (var response = GetResponse(areq))
                    using (var sr = new StreamReader(response.GetResponseStream()))
                        await sr.ReadToEndAsync().ConfigureAwait(false);
            }

            // finish session and commit
            try
            {
                var usfa = new UploadSessionFinishArg();
                usfa.cursor.session_id = ussr.session_id;
                usfa.cursor.offset     = globalBytesRead;
                usfa.commit.path       = path;

                req = CreateRequest(WebApi.Dropbox.UploadSessionFinishUrl(), "POST");
                req.Headers[API_ARG_HEADER] = JsonConvert.SerializeObject(usfa);
                req.ContentType             = "application/octet-stream";
                req.Timeout = 200000;

                return(ReadJSONResponse <FileMetaData>(req));
            }
            catch (Exception ex)
            {
                HandleDropboxException(ex, true);
                throw;
            }
        }
Exemple #3
0
        public FileMetaData UploadFile(String path, Stream stream)
        {
            // start a session
            UploadSessionStartArg ussa = new UploadSessionStartArg();

            var url = string.Format("{0}/files/upload_session/start", CONTENT_API_URL);
            HttpWebRequest req = CreateRequest(url, "POST");
            req.Headers[API_ARG_HEADER] = JsonConvert.SerializeObject(ussa);
            req.ContentType = "application/octet-stream";
            req.ContentLength = Math.Min(DROPBOX_MAX_CHUNK_UPLOAD,stream.Length);
            req.Timeout = 200000;

            var areq = new AsyncHttpRequest(req);

            byte[] buffer = new byte[Utility.Utility.DEFAULT_BUFFER_SIZE];

            UInt64 globalBytesRead = 0;
            using (var rs = areq.GetRequestStream())
            {
                int bytesRead = 0;
                do
                {
                    bytesRead = stream.Read(buffer, 0, (int)Utility.Utility.DEFAULT_BUFFER_SIZE);
                    globalBytesRead += (UInt64)bytesRead;
                    rs.Write(buffer, 0, bytesRead);

                }
                while (bytesRead > 0 && globalBytesRead < DROPBOX_MAX_CHUNK_UPLOAD);

            }

            //Console.WriteLine(((HttpWebResponse)areq.GetResponse()).StatusCode);

            UploadSessionStartResult ussr = ReadJSONResponse<UploadSessionStartResult>(areq); // pun intended

            // keep appending until finished
            // 1) read into buffer
            while (globalBytesRead < (UInt64)stream.Length)
            {

                UInt64 remaining = (UInt64)stream.Length - globalBytesRead;

                // start an append request
                UploadSessionAppendArg usaa = new UploadSessionAppendArg();
                usaa.cursor.session_id = ussr.session_id;
                usaa.cursor.offset = globalBytesRead;
                usaa.close = remaining < DROPBOX_MAX_CHUNK_UPLOAD;
                url = string.Format("{0}/files/upload_session/append_v2", CONTENT_API_URL);

                req = CreateRequest(url, "POST");
                req.Headers[API_ARG_HEADER] = JsonConvert.SerializeObject(usaa);
                req.ContentType = "application/octet-stream";
                req.ContentLength = Math.Min(DROPBOX_MAX_CHUNK_UPLOAD, (long)remaining);
                req.Timeout = 200000;

                areq = new AsyncHttpRequest(req);

                UInt64 bytesReadInRequest = 0;
                using (var rs = areq.GetRequestStream())
                {
                    int bytesRead = 0;
                    do
                    {
                        bytesRead = stream.Read(buffer, 0, (int)Utility.Utility.DEFAULT_BUFFER_SIZE);
                        bytesReadInRequest += (UInt64)bytesRead;
                        globalBytesRead += (UInt64)bytesRead;
                        rs.Write(buffer, 0, bytesRead);

                    }
                    while (bytesRead > 0 && bytesReadInRequest < Math.Min(remaining, DROPBOX_MAX_CHUNK_UPLOAD));
                }
                HttpWebResponse response = GetResponse(areq);
                StreamReader sr = new StreamReader(response.GetResponseStream());
                sr.ReadToEnd();

            }

            // finish session and commit
            try
            {
                UploadSessionFinishArg usfa = new UploadSessionFinishArg();
                usfa.cursor.session_id = ussr.session_id;
                usfa.cursor.offset = (UInt64)globalBytesRead;
                usfa.commit.path = path;

                url = string.Format("{0}/files/upload_session/finish", CONTENT_API_URL);
                req = CreateRequest(url, "POST");
                req.Headers[API_ARG_HEADER] = JsonConvert.SerializeObject(usfa);
                req.ContentType = "application/octet-stream";
                req.Timeout = 200000;

                areq = new AsyncHttpRequest(req);

                //using (var rs = areq.GetRequestStream())
                //{
                //    int bytesRead = 0;
                //    do
                //    {
                //        bytesRead = stream.Read(buffer, 0, (int) Utility.Utility.DEFAULT_BUFFER_SIZE);
                //        globalBytesRead += (UInt64)bytesRead;
                //        rs.Write(buffer, 0, bytesRead);

                //    } while (bytesRead > 0);
                //}
                FileMetaData fmd = ReadJSONResponse<FileMetaData>(areq);
                return fmd;
            }
            catch (Exception ex)
            {
                handleDropboxException(ex);
                throw;
            }
        }