Esempio n. 1
0
        /// <summary>
        /// Creates or updates a given file resource in the file system.
        /// </summary>
        public VirtualFileInfo Post([Optional] Stream input, string filePath)
        {
            //in case of an empty data block, there is no stream - just use a null stream instead
            if (input == null)
            {
                input = Stream.Null;
            }

            VfsHttpHeaders headers = VfsHttpHeaders.Default;

            //get custom headers
            bool overwrite      = Convert.ToBoolean(Request.Headers[headers.OverwriteExistingResource]);
            long resourceLength = Request.Headers.ContentLength ?? 0;

            var    ct          = Request.Headers.ContentType;
            string contentType = ct == null ? ContentUtil.UnknownContentType : ct.Name;

            if (String.IsNullOrEmpty(contentType))
            {
                contentType = ContentUtil.UnknownContentType;
            }

            //wrap OpenRasta stream into a non-seekable one - the OR streams indicates it's seekable
            //but does not support setting its position
            var stream = new NonSeekableStream(input);

            return(FileSystem.WriteFile(filePath, stream, overwrite, resourceLength, contentType));
        }
        /// <summary>
        /// Creates or updates a given file resource in the file system in one blocking operation.
        /// </summary>
        /// <param name="virtualFilePath">The qualified path of the file to be created.</param>
        /// <param name="input">A stream that provides the file's contents.</param>
        /// <param name="overwrite">Whether an existing file should be overwritten
        /// or not. If this parameter is false and the file already exists, a
        /// <see cref="ResourceOverwriteException"/> is thrown.</param>
        /// <param name="resourceLength">The length of the resource to be uploaded in bytes.</param>
        /// <param name="contentType">The content type of the uploaded resource.</param>
        /// <exception cref="ResourceAccessException">In case of invalid or prohibited
        /// resource access.</exception>
        /// <exception cref="ResourceOverwriteException">If a file already exists at the
        /// specified location, and the <paramref name="overwrite"/> flag was not set.</exception>
        /// <exception cref="ArgumentNullException">If any of the parameters is a null reference.</exception>
        public void WriteFile(string virtualFilePath, Stream input, bool overwrite, long resourceLength, string contentType)
        {
            string actionUri = VfsUris.Default.WriteFileContentsUri;

            actionUri = actionUri.ConstructUri(Uris.PatternFilePath, virtualFilePath);

            //set headers
            RequestHeaders requestHeader = new RequestHeaders();
            VfsHttpHeaders headers       = VfsHttpHeaders.Default;

            requestHeader.Add(headers.OverwriteExistingResource, overwrite.ToString(CultureInfo.InvariantCulture));
            requestHeader.Add("Content-Length", resourceLength.ToString(CultureInfo.InvariantCulture));
            requestHeader.Add("Content-Type", contentType);


            Func <HttpClient, HttpResponseMessage> func = c =>
            {
                HttpContent content = HttpContent.Create(() => input);
                return(c.Send(HttpMethod.POST, actionUri, requestHeader,
                              content));
            };

            SecureRequest(FileSystemTask.StreamedFileUploadRequest,
                          func,
                          () => String.Format("Could not write data for file [{0}] to file system.", virtualFilePath));
        }
        /// <summary>
        /// Posts the data of a given data block to the server.
        /// </summary>
        private HttpResponseMessage SendDataBlock(HttpClient client, StreamedDataBlock dataBlock)
        {
            string actionUri = VfsUris.Default.WriteStreamedDataBlockUri;

            actionUri = actionUri.ConstructUri(Uris.PatternTransferId, dataBlock.TransferTokenId);
            actionUri = actionUri.ConstructUri(Uris.PatternBlockNumber, dataBlock.BlockNumber.ToString(CultureInfo.InvariantCulture));

            RequestHeaders requestHeader = new RequestHeaders();

            //write the HTTP headers
            VfsHttpHeaders headers = VfsHttpHeaders.Default;

            requestHeader.Add(headers.TransferId, dataBlock.TransferTokenId); //duplicate transfer ID, might be useful in some scenarios
            requestHeader.Add(headers.BlockNumber, dataBlock.BlockNumber.ToString());
            requestHeader.Add(headers.IsLastBlock, Convert.ToString(dataBlock.IsLastBlock).ToLowerInvariant());
            requestHeader.Add(headers.BlockOffset, dataBlock.Offset.ToString());

            if (dataBlock.BlockLength.HasValue)
            {
                requestHeader.Add(headers.BlockLength, dataBlock.BlockLength.ToString());
            }

            using (dataBlock.Data)
            {
                return(client.Send(HttpMethod.POST, actionUri, requestHeader, HttpContent.Create(() => dataBlock.Data)));
            }
        }
Esempio n. 4
0
        public void WriteTo(object entity, IHttpEntity response, string[] codecParameters)
        {
            //get the data block to be transferred
            StreamedDataBlock dataBlock = (StreamedDataBlock)entity;


            if (HttpContext.Current != null)
            {
                //disable buffering
                HttpContext.Current.Response.BufferOutput = false;

                if (dataBlock.BlockLength.HasValue)
                {
                    //only set the content lengths, if we actually know it
                    response.SetHeader("Content-Length", dataBlock.BlockLength.Value.ToString(CultureInfo.InvariantCulture));
                }

                response.SetHeader("Content-Type", MediaType.ApplicationOctetStream.Name);
            }
            else
            {
                response.ContentLength = dataBlock.BlockLength;
                response.ContentType   = MediaType.ApplicationOctetStream;
            }

            //write the HTTP headers
            VfsHttpHeaders headers = VfsHttpHeaders.Default;

            response.SetHeader(headers.TransferId, dataBlock.TransferTokenId);
            response.SetHeader(headers.BlockLength, dataBlock.BlockLength.ToString());
            response.SetHeader(headers.BlockNumber, dataBlock.BlockNumber.ToString());
            response.SetHeader(headers.IsLastBlock, dataBlock.IsLastBlock.ToString());
            response.SetHeader(headers.BlockOffset, dataBlock.Offset.ToString());

            using (dataBlock.Data)
            {
                //write data to response stream, then close the stream
                dataBlock.Data.CopyTo(response.Stream);
            }
        }
        private void WriteBlock(string transferId, long blockNumber, Stream input)
        {
            //get meta information from headers
            VfsHttpHeaders       headerNames = VfsHttpHeaders.Default;
            HttpHeaderDictionary headers     = Request.Headers;

            var blockLength = Convert.ToInt32(headers[headerNames.BlockLength]);
            var isLastBlock = headers.ContainsKey(headerNames.IsLastBlock) ? Convert.ToBoolean(headers[headerNames.IsLastBlock]) : false;
            var offset      = Convert.ToInt64(headers[headerNames.BlockOffset]);

            StreamedDataBlock block = new StreamedDataBlock
            {
                TransferTokenId = transferId,
                BlockNumber     = blockNumber,
                IsLastBlock     = isLastBlock,
                BlockLength     = blockLength,
                Offset          = offset,
                Data            = new NonSeekableStream(input)
            };

            FileSystem.UploadTransfers.WriteBlockStreamed(block);
        }