Example #1
0
        public void Add(string name, string value)
        {
            var content = new SimpleContent(Encoding.UTF8.GetBytes(value));

            content.Headers.Add("Content-Disposition", "form-data; name=\"" + name + "\"");
            base.Add(content);
        }
Example #2
0
        public HttpContent AsContent(Command cmd, IServerContext context, bool multipart)
        {
            _data.Position = 0;
            HttpContent result;

#if FILEIO
            if (_data == null)
            {
                result = new SimpleContent(new FileStream(_filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 4096));
            }
            else
            {
                result = new SimpleContent(_data);
            }
#else
            result = new SimpleContent(_data);
#endif

            var id   = _id[0] == '@' ? cmd.Substitute(_id, context) : _id;
            var path = GetFileName(_path[0] == '@' ? cmd.Substitute(_path, context) : _path);
            if (multipart)
            {
                result.Headers.Add("Content-Disposition", string.Format("form-data; name=\"{0}\"; filename=\"{1}\"", id, path));
            }
            else
            {
                result.Headers.Add("Content-Disposition", "attachment; filename*=" + Encode5987(path));
                result.Headers.Add("Content-Range", string.Format("bytes {0}-{1}/{2}", 0, _length - 1, _length));

                var hash = default(byte[]);

                _data.Position = 0;
#if FILEIO
                if (_data == null)
                {
                    hash = new xxHash(32).ComputeHash(new FileStream(_filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 4096));
                }
                else
                {
                    hash = new xxHash(32).ComputeHash(_data);
                }
#else
                hash = new xxHash(32).ComputeHash(_data);
#endif
                _data.Position = 0;
                var hashStr = BitConverter.ToUInt32(hash, 0).ToString(CultureInfo.InvariantCulture);
                result.Headers.Add("Aras-Content-Range-Checksum", hashStr);
                result.Headers.Add("Aras-Content-Range-Checksum-Type", "xxHashAsUInt32AsDecimalString");
            }
            return(result);
        }
        public IEnumerable <HttpContent> AsContent(Command cmd, IServerContext context, bool multipart)
        {
            var stream = _data;

            if (stream?.CanSeek == true)
            {
                stream.Position = 0;
            }
            var disposeLast = false;

#if FILEIO
            if (stream == null)
            {
                stream      = new FileStream(_filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 4096);
                disposeLast = true;
            }
#endif

            const long chunkSize = 16 * 1024 * 1024;
            var        numChunks = _length.HasValue && !multipart ? (int)Math.Ceiling((double)_length / chunkSize) : 1;
            var        results   = new HttpContent[numChunks];

            for (var i = 0; i < numChunks; i++)
            {
                var chunkLength = _length;
                if (_length.HasValue && numChunks > 1)
                {
                    chunkLength = Math.Min(chunkSize, _length.Value - i * chunkSize);
                    results[i]  = new FileStreamContent(stream, chunkLength.Value, disposeLast && i == numChunks - 1);
                }
                else
                {
                    results[i] = new SimpleContent(stream, disposeLast);
                }

                var id   = _id[0] == '@' ? cmd.Substitute(_id, context) : _id;
                var path = GetFileName(_path[0] == '@' ? cmd.Substitute(_path, context) : _path);
                if (multipart)
                {
                    results[i].Headers.Add("Content-Disposition", string.Format("form-data; name=\"{0}\"; filename=\"{1}\"", id, path));
                }
                else
                {
                    results[i].Headers.Add("Content-Disposition", "attachment; filename*=" + Encode5987(path));
                    if (chunkLength.HasValue && chunkLength.Value > 0)
                    {
                        results[i].Headers.Add("Content-Range", string.Format("bytes {0}-{1}/{2}", i * chunkSize, i * chunkSize + chunkLength - 1, _length));
                    }

                    if (numChunks == 1)
                    {
                        var hash = default(byte[]);
#if FILEIO
                        if (_data == null)
                        {
                            using (var s = new FileStream(_filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 4096))
                                hash = new xxHash(32).ComputeHash(s);
                        }
                        else if (_data.CanSeek)
                        {
                            _data.Position = 0;
                            hash           = new xxHash(32).ComputeHash(_data);
                            _data.Position = 0;
                        }
#else
                        if (_data.CanSeek)
                        {
                            _data.Position = 0;
                            hash           = new xxHash(32).ComputeHash(_data);
                            _data.Position = 0;
                        }
#endif
                        if (hash != null)
                        {
                            var hashStr = BitConverter.ToUInt32(hash, 0).ToString(CultureInfo.InvariantCulture);
                            results[i].Headers.Add("Aras-Content-Range-Checksum", hashStr);
                            results[i].Headers.Add("Aras-Content-Range-Checksum-Type", "xxHashAsUInt32AsDecimalString");
                        }
                    }
                }
            }

            return(results);
        }