Esempio n. 1
0
        internal override async Task DoCommandAsync()
        {
            if (FileManager.GetInstanse().GetFileInfo(_mFileName).Exists)
            {
                FileManager.GetInstanse().DeleteFileOrDir(_mFileName);
            }
            if (FileManager.GetInstanse().GetDirInfo(_mFileName).Exists)
            {
                FileManager.GetInstanse().DeleteFileOrDir(_mFileName);
            }
            _code = HttpStatusCodes.SuccessCreated;
            var  progress = new ProgressView(Console.BufferWidth);
            long sum      = 0;

            using (var file = FileManager.GetInstanse().CreateFile(_mFileName)) {
                var buffer = new byte[ProgramCostants.DefautBufferSize];
                try {
                    while (true)
                    {
                        var count = await _fileStream.ReadAsync(buffer, 0, buffer.Length);

                        if (count > 0)
                        {
                            sum += count;
                            await file.WriteAsync(buffer, 0, count);

                            if (_size > ProgramCostants.MinProgressViewingSize)
                            {
                                progress.DrawProgress((double)sum / _size);
                            }
                        }
                        else
                        {
                            Console.WriteLine();
                            break;
                        }
                    }
                } catch (HttpListenerException e) {
                    Console.WriteLine(e.Message);
                }
                _fileStream.Close();
                _code = HttpStatusCodes.SuccessCreated;
            }
        }
Esempio n. 2
0
        internal virtual async void SetResponse(HttpListenerResponse response)
        {
            if (null == response)
            {
                throw new ArgumentNullException("response");
            }
            response.ContentLength64 = _contentLength;
            response.StatusCode      = _mCode;
            var keys = _headers.Keys;

            foreach (var key in keys)
            {
                var val = _headers[key];
                if (key.Equals("allow"))
                {
                    response.AddHeader(key, "OPTIONS,GET,HEAD,POST,DELETE,PROPFIND,PROPPATCH,COPY,MOVE,LOCK,UNLOCK");
                }
                else
                {
                    response.AppendHeader(key, val);
                }
            }
            if (null == _mData)
            {
                response.OutputStream.Close();
                return;
            }
            var  buffer   = new byte[ProgramCostants.DefautBufferSize];
            var  progress = new ProgressView(Console.BufferWidth);
            long sum      = 0;

            using (var output = response.OutputStream) {
                try {
                    while (true)
                    {
                        var count = await _mData.ReadAsync(buffer, 0, buffer.Length);

                        if (count > 0)
                        {
                            sum += count;
                            await output.WriteAsync(buffer, 0, count);

                            if (_contentLength > ProgramCostants.MinProgressViewingSize)
                            {
                                progress.DrawProgress((double)sum / _contentLength);
                            }
                        }
                        else
                        {
                            Console.WriteLine();
                            break;
                        }
                        if (sum == _contentLength)
                        {
                            break;
                        }
                    }
                } catch (HttpListenerException e) {
                    // error
                    Logger.Error(e.Message);
                }
                _mData.Close();
            }
            response.Close();
        }