Ejemplo n.º 1
0
        private void WriteFilePart(IHttpStreamable httpStream, bool prepare, string key, FileLink value)
        {
            FileInfo fileInfo = (FileInfo)value.GetInfo();
            string   fileName = fileInfo.Name;

            if (NoMediaExtension)
            {
                fileName = StringFunc.Substring(fileName, 0, fileName.IndexOf(fileInfo.Extension));
            }
            httpStream.WriteLine("Content-Disposition: form-data; name=\"" + key + "\"; filename=\"" + fileName + "\"");
            httpStream.WriteLine("Content-Type: " + MimeType.GetByFile(value).Notation);

            if (!Chunked)
            {
                httpStream.WriteLine("Content-Transfer-Encoding: binary");
            }
            httpStream.WriteLine();
            httpStream.FlushUnderlying();

            if (!prepare)
            {
                WriteFile(httpStream, value);
            }
            httpStream.WriteLine();
            httpStream.FlushUnderlying();
        }
Ejemplo n.º 2
0
 private void WriteOctetStreamPart(IHttpStreamable httpStream, string key)
 {
     httpStream.WriteLine("Content-Disposition: form-data; name=\"" + key + "\"; filename=\"\"");
     httpStream.WriteLine("Content-Type: application/octet-stream");
     httpStream.WriteLine();
     httpStream.WriteLine();
     httpStream.FlushUnderlying();
 }
Ejemplo n.º 3
0
 private void WriteStringPart(IHttpStreamable httpStream, string key, string value)
 {
     httpStream.WriteLine("Content-Disposition: form-data; name=\"" + key + "\"");
     //httpStream.writeLine("Content-Type: text/plain; charset=UTF-8");
     httpStream.WriteLine();
     httpStream.WriteLine(value);
     httpStream.FlushUnderlying();
 }
Ejemplo n.º 4
0
        protected override void Build(IHttpStreamable httpStream, bool prepare)
        {
            fileSize = 0;
            bool lastEntryWasFile = false;

            foreach (AttributeKeyValuePair kvp in kvpContainer.Kvps)
            {
                string key = Encoding.UTF8.GetString(Encoding.Default.GetBytes(kvp.Key));
                lastEntryWasFile = true;

                httpStream.WriteLine("--" + Boundary);

                if (kvp.Value is null)
                {
                    WriteOctetStreamPart(httpStream, key);
                    continue;
                }
                if (kvp.Value is byte[])
                {
                    WriteOctetStreamPart(httpStream, prepare, key, (byte[])kvp.Value);
                    fileSize += ((byte[])kvp.Value).Length;
                    continue;
                }
                if (kvp.Value is FileLink)
                {
                    if (Chunked)
                    {
                        if (ChunkHandler is null)
                        {
                            ChunkHandler = new ChunkHandler((FileLink)kvp.Value);
                        }
                        ChunkHandler.PrepareTransfer();
                        fileSize += ChunkHandler.ChunkSize;
                    }
                    else
                    {
                        fileSize += ((FileInfo)((FileLink)kvp.Value).GetInfo()).Length;
                    }
                    WriteFilePart(httpStream, prepare, key, (FileLink)kvp.Value);
                    continue;
                }
                string value = kvp.Value is string?(string)kvp.Value : kvp.Value.ToString();
                value = Encoding.UTF8.GetString(Encoding.Default.GetBytes(value));

                lastEntryWasFile = false;

                WriteStringPart(httpStream, key, value);
            }
            if (lastEntryWasFile)
            {
                httpStream.WriteLine();
                httpStream.FlushUnderlying();
            }
            httpStream.WriteLine("--" + Boundary + "--");
        }
Ejemplo n.º 5
0
        private void WriteOctetStreamPart(IHttpStreamable httpStream, bool prepare, string key, byte[] payload)
        {
            httpStream.WriteLine("Content-Disposition: form-data; name=\"" + key + "\"; filename=\"blob\"");
            httpStream.WriteLine("Content-Type: application/octet-stream");
            httpStream.WriteLine();

            if (!prepare)
            {
                httpStream.Write(payload);
            }
            httpStream.WriteLine();
            httpStream.FlushUnderlying();
        }
Ejemplo n.º 6
0
        private void WriteFile(IHttpStreamable httpStream, FileLink fileLink)
        {
            if (Chunked)
            {
                ChunkHandler.WriteChunk(httpStream);
                httpStream.FlushUnderlying();

                if (ChunkHandler.IsFinished)
                {
                    ChunkHandler.Dispose();
                }
                return;
            }
            using (FileStream fis = new FileStream(fileLink.Path, FileMode.Open)) {
                int    bytesRead = -1;
                byte[] buffer    = new byte[HttpConfig.ChunkSize];

                while ((bytesRead = fis.Read(buffer)) != -1)
                {
                    httpStream.WriteUnderlying(buffer, 0, bytesRead);
                }
                httpStream.FlushUnderlying();
            }
        }
Ejemplo n.º 7
0
        protected override void Build(IHttpStreamable httpStream, bool prepare)
        {
            if (prepare)
            {
                return;
            }
            using (FileStream fis = new FileStream(fileLink.Path, FileMode.Open)) {
                int    bytesRead = -1;
                byte[] buffer    = new byte[HttpConfig.ChunkSize];

                while ((bytesRead = fis.Read(buffer, bytesRead, buffer.Length)) != -1)
                {
                    httpStream.WriteUnderlying(buffer, 0, bytesRead);
                }
                httpStream.FlushUnderlying();
            }
        }