Ejemplo n.º 1
0
        private async Task HandleSuccess()
        {
            await UploadUtil.SendResp(_ws, Resp.Ok(MakeDownloadUrl()));

            Log.Information("Upload succeeded.");
            await _ws.CloseAsync(WebSocketCloseStatus.NormalClosure, "Success", CancellationToken.None);
        }
Ejemplo n.º 2
0
        public async Task Run()
        {
            try {
                _writer.RunAsync();
                for (int bytesReceived = 0; bytesReceived < _targetFileSize;)
                {
                    var buf = new byte[1024 * 128]; // 128 KiB

                    WebSocketReceiveResult result =
                        await _ws.ReceiveAsync(new ArraySegment <byte>(buf), CancellationToken.None);

                    bytesReceived += result.Count;


                    await UploadUtil.SendResp(_ws, Resp.Ok().WithValueLong(result.Count));


                    _writer.Process(new ArraySegment <byte>(buf, 0, result.Count));
                }
            } catch (Exception e) {
                Log.Error("Error while processing file: {exception}", e);
                _writer.Cancel();
            }

            _sentFinalResponseNotification.WaitOne();
        }
        private async Task <SetupMsg> Setup(WebSocket ws)
        {
            var buf = new byte[256];
            // Read setup msg
            WebSocketReceiveResult result = await ws.ReceiveAsync(new ArraySegment <byte>(buf), CancellationToken.None);

            SetupMsg setupMsg = JsonConvert.DeserializeObject <SetupMsg>(Encoding.UTF8.GetString(buf, 0, result.Count));

            if (setupMsg.FileSize > UploadConfig.MaxFileSize)
            {
                await ws.CloseAsync(WebSocketCloseStatus.PolicyViolation, "File size too large",
                                    CancellationToken.None);

                throw new Exception("File size too large");
            }

            setupMsg.FileName = UploadUtil.SanitizeFileName(setupMsg.FileName);
            setupMsg.Code     = GetGuid();
            _dbHelper.InsertFilesEntry(Files.CreateWithUtcNow(setupMsg.Code, setupMsg.FileName, setupMsg.FileSize));
            await UploadUtil.SendResp(ws, Resp.Ok());

            return(setupMsg);
        }