public virtual WebRequestStream GetRequestStreamMultiPartUpload(WebRequest request, String fileName, long fileSize)
        {
            // generate mp support
            WebRequestMultipartFormDataSupport mp = new WebRequestMultipartFormDataSupport();

            // set the right size
            fileSize = fileSize + mp.GetHeaderFooterSize(fileName);

            // set the streaming buffering
#if !WINDOWS_PHONE
            if (fileSize > 0)
            {
                // set the maximum content length size
                request.ContentLength = fileSize;
            }
#endif
            // get the stream
            WebRequestStream stream = GetRequestStream(request, fileSize);

            // prepare the stream
            mp.PrepareRequestStream(stream, fileName);

            // go ahead
            return(stream);
        }
Exemplo n.º 2
0
        /// <summary>
        /// This method returns a webrequest data stream and should be used in
        /// a using clause
        /// </summary>
        /// <param name="request"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public virtual WebRequestStream GetRequestStream(WebRequest request, long length)
        {
            // set the conten length
            request.ContentLength = length;

            // get the network stream
            var nwStream = WebRequestStreamHelper.GetRequestStream(request);

            // return the request streaM;
            var ws = new WebRequestStream(nwStream, request, this);

            // add pre dispose opp
            ws.PushPreDisposeOperation(DisposeWebRequestStreams, request, ws);

            // go ahead
            return(ws);
        }
        /// <summary>
        /// This method contains the routine which has to be executed when a request
        /// stream will be disposed and will be called autoamtically
        /// </summary>
        private void DisposeWebRequestStreams(params object[] arg)
        {
            // get the params
            WebRequest       request = arg[0] as WebRequest;
            WebRequestStream stream  = arg[1] as WebRequestStream;

            // check if we have a multipart upload
            WebRequestMultipartFormDataSupport md = new WebRequestMultipartFormDataSupport();

            if (md.IsRequestMultiPartUpload(request))
            {
                md.FinalizeNetworkFileDataStream(stream);
            }

#if WINDOWS_PHONE
            // Close Stream Bugfix for WP7
            stream.Close();
#endif
        }
        public override Stream CreateUploadStream(IStorageProviderSession session, ICloudFileSystemEntry entry, long uploadSize)
        {
            if (entry is ICloudDirectoryEntry)
                throw new ArgumentException("Upload operation can be perform for files only");

            var tempStream = new FileStream(Path.GetTempFileName(), FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None, 4096, FileOptions.DeleteOnClose);
            var uploadStream = new WebRequestStream(tempStream, null, null);
            uploadStream.PushPreDisposeOperation(CommitUploadOperation, tempStream, uploadSize, session, entry);
            return uploadStream;
        }
 public override Stream CreateUploadStream(IStorageProviderSession session, ICloudFileSystemEntry fileSystemEntry, long uploadSize)
 {
     var tempStream = new FileStream(Path.GetTempFileName(), FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None, 4096, FileOptions.DeleteOnClose);
     var stream = new WebRequestStream(tempStream, null, null);
     stream.PushPreDisposeOperation(CommitUploadOperation, session, fileSystemEntry, tempStream, uploadSize);
     return stream;
 }
        public override Stream CreateUploadStream(IStorageProviderSession session, ICloudFileSystemEntry fileSystemEntry, long uploadSize)
        {
            String url;
            bool update = false;

            String type = GetMimeType(fileSystemEntry.Name);

            if (GoogleDocsResourceHelper.IsResorceId(fileSystemEntry.Id)) //update existent
            {
                var ext = GoogleDocsResourceHelper.GetExtensionByKind(fileSystemEntry.GetPropertyValue(GoogleDocsConstants.KindProperty));
                if (!String.IsNullOrEmpty(ext))
                {
                    //for google docs kind we previously add an extension that don't needed anymore
                    var index = fileSystemEntry.Name.IndexOf('.' + ext, StringComparison.Ordinal);
                    (fileSystemEntry as BaseFileEntry).Name = fileSystemEntry.Name.Substring(0, index);
                }

                url = fileSystemEntry.GetPropertyValue(GoogleDocsConstants.ResEditMediaProperty);
                update = true;

                if (String.IsNullOrEmpty(url))
                    throw new HttpException(403, "User not authorize to update resource");
            }
            else
            {
                url =  fileSystemEntry.Parent.GetPropertyValue(GoogleDocsConstants.ResCreateMediaProperty) + "?convert=false";
            }

            //first initiate resumable upload request
            var initRequest = CreateWebRequest(session, url, update ? "PUT" : "POST", null);
            initRequest.Headers.Add("X-Upload-Content-Length", uploadSize.ToString(CultureInfo.InvariantCulture));
            initRequest.Headers.Add("X-Upload-Content-Type", type);
            if (update)
            {
                initRequest.Headers.Add("If-Match", "*");
            }
            GoogleDocsXmlParser.WriteAtom(initRequest, GoogleDocsXmlParser.EntryElement(null, GoogleDocsXmlParser.TitleElement(fileSystemEntry.Name)));

            var response = initRequest.GetResponse();

            //secondly create request to obtained url
            var uplRequest = CreateWebRequest(session, response.Headers["Location"], "PUT", null);
            uplRequest.ContentLength = uploadSize;
            uplRequest.ContentType = type;
            uplRequest.Headers.Add("Content-Range", String.Format("bytes {0}-{1}/{2}", 0, uploadSize - 1, uploadSize));

            var wrStream = new WebRequestStream(uplRequest.GetRequestStream(), uplRequest, null);
            wrStream.PushPostDisposeOperation(CommitUploadOperation, session, uplRequest, fileSystemEntry);

            return wrStream;
        }
        public override Stream CreateUploadStream(IStorageProviderSession session, ICloudFileSystemEntry entry, long uploadSize)
        {
            if (entry is ICloudDirectoryEntry)
                throw new ArgumentException("Upload operation can be perform for files only");

            String uri = String.Format("{0}/{1}/files/{2}", SkyDriveConstants.BaseAccessUrl, entry.ParentID, entry.Name);
            uri = SignUri(session, uri);

            WebRequest request = WebRequest.Create(uri);
            request.Method = "PUT";
            request.ContentLength = uploadSize;

            if (entry is BaseFileEntry)
                (entry as BaseFileEntry).Length = uploadSize;

            var stream = new WebRequestStream(request.GetRequestStream(), request, null);
            stream.PushPostDisposeOperation(CommitUploadOperation, request, entry);

            return stream;
        }
        /// <summary>
        /// This method returns a webrequest data stream and should be used in 
        /// a using clause
        /// </summary>
        /// <param name="request"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public virtual WebRequestStream GetRequestStream(WebRequest request, long length)
        {
#if !WINDOWS_PHONE
            // set the conten length
            request.ContentLength = length;
#endif
            
            // get the network stream
            Stream nwStream = WebRequestStreamHelper.GetRequestStream(request);

            // return the request streaM;
            WebRequestStream ws =  new WebRequestStream(nwStream, request, this);

            // add pre dispose opp
            ws.PushPreDisposeOperation(DisposeWebRequestStreams, request, ws);

            // go ahead
            return ws;
        }