예제 #1
0
            public AsyncWrapper(AsyncHttpRequest owner, bool isRequest)
            {
                m_owner     = owner;
                m_isRequest = isRequest;

                if (m_isRequest)
                {
                    m_async = m_owner.m_request.BeginGetRequestStream(new AsyncCallback(this.OnAsync), null);
                }
                else
                {
                    m_async = m_owner.m_request.BeginGetResponse(new AsyncCallback(this.OnAsync), null);
                }

                if (m_owner.m_timeout != System.Threading.Timeout.Infinite)
                {
                    ThreadPool.RegisterWaitForSingleObject(m_async.AsyncWaitHandle, new WaitOrTimerCallback(this.OnTimeout), null, TimeSpan.FromMilliseconds(m_owner.m_timeout), true);
                }
            }
예제 #2
0

        
예제 #3
0
        public void Rename(string oldname, string newname)
        {
            var data = System.Text.Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(new BucketResourceItem() {
                name = m_prefix + newname,
            }));

            var url = string.Format("{0}/b/{1}/o/{2}", API_URL, m_bucket, Library.Utility.Uri.UrlPathEncode(m_prefix + oldname));
            var req = m_oauth.CreateRequest(url);
            req.Method = "PATCH";
            req.ContentLength = data.Length;
            req.ContentType = "application/json; charset=UTF-8";

            var areq = new AsyncHttpRequest(req);
            using(var rs = areq.GetRequestStream())
                rs.Write(data, 0, data.Length);

            m_oauth.ReadJSONResponse<BucketResourceItem>(req);
        }
예제 #4
0
        public void CreateFolder()
        {
            if (string.IsNullOrEmpty(m_project))
                throw new Exception(Strings.GoogleCloudStorage.ProjectIDMissingError(PROJECT_OPTION));

            var data = System.Text.Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(new CreateBucketRequest() {
                name = m_bucket,
                location = m_location,
                storageClass = m_storage_class
            }));

            var url = string.Format("{0}/b?project={1}", API_URL, m_project);

            var req = m_oauth.CreateRequest(url);
            req.Method = "POST";
            req.ContentLength = data.Length;
            req.ContentType = "application/json; charset=UTF-8";

            var areq = new AsyncHttpRequest(req);

            using(var rs = areq.GetRequestStream())
                rs.Write(data, 0, data.Length);

            m_oauth.ReadJSONResponse<BucketResourceItem>(areq);
        }
예제 #5
0
        public void Get(string remotename, System.IO.Stream stream)
        {
            try
            {
                var url = string.Format("{0}/b/{1}/o/{2}?alt=media", API_URL, m_bucket, Library.Utility.Uri.UrlPathEncode(m_prefix + remotename));
                var req = m_oauth.CreateRequest(url);
                var areq = new AsyncHttpRequest(req);

                using(var resp = areq.GetResponse())
				using(var rs = areq.GetResponseStream())
                    Library.Utility.Utility.CopyStream(rs, stream);
            }
            catch (WebException wex)
            {
                if (wex.Response is HttpWebResponse && ((HttpWebResponse)wex.Response).StatusCode == HttpStatusCode.NotFound)
                    throw new FileMissingException();
                else
                    throw;
            }

        }
예제 #6
0

        
예제 #7
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;
            }
        }
예제 #8
0
        public void Get(string remotename, System.IO.Stream stream)
        {
            // Prevent repeated download url lookups
            if (m_filecache.Count == 0)
                List();

            var fileid = GetFileEntries(remotename).OrderByDescending(x => x.createdDate).First().id;

            var req = m_oauth.CreateRequest(string.Format("{0}/files/{1}?alt=media", DRIVE_API_URL, fileid));
            var areq = new AsyncHttpRequest(req);
            using(var resp = (HttpWebResponse)areq.GetResponse())
            using(var rs = resp.GetResponseStream())
                Duplicati.Library.Utility.Utility.CopyStream(rs, stream);
        }
예제 #9
0
            public AsyncWrapper(AsyncHttpRequest owner, bool isRequest)
            {
                m_owner = owner;
                m_isRequest = isRequest;

                if (m_isRequest)
                    m_async = m_owner.m_request.BeginGetRequestStream(new AsyncCallback(this.OnAsync), null);
                else
                    m_async = m_owner.m_request.BeginGetResponse(new AsyncCallback(this.OnAsync), null);

                if ( m_owner.m_timeout != System.Threading.Timeout.Infinite)
                    ThreadPool.RegisterWaitForSingleObject(m_async.AsyncWaitHandle, new WaitOrTimerCallback(this.OnTimeout), null, TimeSpan.FromMilliseconds( m_owner.m_timeout), true);
            }
예제 #10
0
파일: B2.cs 프로젝트: AlexFRAN/duplicati
        public void Get(string remotename, System.IO.Stream stream)
        {
            AsyncHttpRequest req;
            if (m_filecache == null || !m_filecache.ContainsKey(remotename))
                List();

            if (m_filecache != null && m_filecache.ContainsKey(remotename))
                req = new AsyncHttpRequest(m_helper.CreateRequest(string.Format("{0}/b2api/v1/b2_download_file_by_id?fileId={1}", m_helper.DownloadUrl, Library.Utility.Uri.UrlEncode(GetFileID(remotename)))));
            else
				req = new AsyncHttpRequest(m_helper.CreateRequest(string.Format("{0}/{1}{2}", m_helper.DownloadUrl, m_urlencodedprefix, Library.Utility.Uri.UrlPathEncode(remotename))));

            try
            {
				using(var resp = req.GetResponse())
				using(var rs = req.GetResponseStream())
                    Library.Utility.Utility.CopyStream(rs, stream);
            }
            catch (Exception ex)
            {
                if (B2AuthHelper.GetExceptionStatusCode(ex) == HttpStatusCode.NotFound)
                    throw new FileMissingException();

                B2AuthHelper.AttemptParseAndThrowException(ex);

                throw;
            }
        }