예제 #1
0
        public async Task <ResponseDTO <bool> > UploadPartFileAsync(FilePartDTO filePart, Guid fileId)
        {
            var response = new ResponseDTO <bool>();
            var file     = await _context.Files.FirstOrDefaultAsync(f => f.Id == fileId);

            var path = Path.Combine(_path, fileId.ToString() + Path.GetExtension(file.Name));

            using (var fs = new FileStream(path, FileMode.Append))
            {
                fs.Write(filePart.Content, 0, filePart.Content.Length);
            }
            file.PartsUploaded++;
            await _context.SaveChangesAsync();

            var message = await _context.Messages.Include(m => m.Room).FirstOrDefaultAsync(m => m.Id == file.MessageId);

            var socketMessage = new FileStatusDTO {
                Id = fileId, MessageId = file.MessageId, PartsUploaded = file.PartsUploaded
            };
            var socketDTO = new SocketResponseDTO <FileStatusDTO> {
                Type = SocketMessageTypes.FileStatusChanged, Model = socketMessage
            };

            foreach (var p in message.Room.Participants.Select(p => p.UserId))
            {
                await _chatHandler.SendMessageByUserId(p, socketDTO);
            }
            return(response);
        }
예제 #2
0
        public async void Upload(FileMessageRequest file, Guid roomId)
        {
            file.Size     = File.OpenRead(file.Path).Length;
            file.PartSize = BufferSize;
            file.Parts    = (int)Math.Ceiling(file.Size / (double)BufferSize);

            var response = await _httpManager.PostAsync <FileDTO>($"api/Rooms/{roomId.ToString()}/FileMessages", file);

            if (response.Error != null)
            {
                Failed?.Invoke(response.Error.ErrorDescription);
                return;
            }

            var position = 0;
            var part     = 1;

            while (position < file.Size)
            {
                byte[] buffer;
                if (position + BufferSize > file.Size)
                {
                    buffer = new byte[(int)file.Size - position];
                }
                else
                {
                    buffer = new byte[BufferSize];
                }

                using (BinaryReader reader = new BinaryReader(new FileStream(file.Path, FileMode.Open, FileAccess.Read)))
                {
                    reader.BaseStream.Seek(position, SeekOrigin.Begin);
                    if (position + BufferSize > file.Size)
                    {
                        reader.Read(buffer, 0, (int)file.Size - position);
                    }
                    else
                    {
                        reader.Read(buffer, 0, BufferSize);
                    }
                }

                var request = new FilePartDTO
                {
                    Content    = buffer,
                    PartNumber = part
                };

                var resp = await _httpManager.PostAsync <bool>($"api/Files/{response.Data.Id}", request);

                if (resp.Error != null)
                {
                    Failed?.Invoke(resp.Error.ErrorDescription);
                    return;
                }

                position += BufferSize;
            }
        }
예제 #3
0
        public async Task <IActionResult> UploadFilePart([FromRoute] Guid id, [FromBody] FilePartDTO file)
        {
            var response = await _fileManager.UploadPartFileAsync(file, id);

            if (response.Error != null)
            {
                return(StatusCode(response.Error.ErrorCode, response));
            }
            return(Ok(response));
        }