private string SendFinished(IWebServer sender, RequestDetails request) { try { var query = QueryHelpers.ParseQuery(request.Url.Query); var success = (query["success"][0].ToLower() == "true"); var message = ""; string[] parts = request.Url.AbsolutePath.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries); var key = parts[0]; if (success) { FileTransferProgressInternal?.Invoke(this, new FileTransferProgressEventArgs { CurrentPart = keyTable[key].lastSliceId + 1, Total = keyTable[key].lastSliceId + 1, State = FileTransferState.Finished }); } else { message = query["message"][0]; FileTransferProgressInternal?.Invoke(this, new FileTransferProgressEventArgs { CurrentPart = keyTable[key].lastSliceId + 1, Total = keyTable[key].lastSliceId + 1, State = FileTransferState.Error, Message = message }); } fileSendTcs?.TrySetResult(message); } catch (Exception ex) { fileSendTcs?.TrySetResult(ex.Message); } return("OK"); }
private async Task <byte[]> GetFileSlice(IWebServer sender, RequestDetails request) { try { string[] parts = request.Url.AbsolutePath.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries); var key = parts[0]; ulong id = ulong.Parse(parts[1]); if (id >= keyTable[key].lastPieceAccessed) { FileTransferProgressInternal?.Invoke(this, new FileTransferProgressEventArgs { CurrentPart = id + 1, Total = keyTable[key].lastSliceId + 1, State = FileTransferState.DataTransfer }); keyTable[key].lastPieceAccessed = (uint)id; } IFile file = keyTable[key].storageFile; int pieceSize = ((keyTable[key].lastSliceId != id) || (keyTable[key].lastSliceSize == 0)) ? (int)Constants.FileSliceMaxLength : (int)keyTable[key].lastSliceSize; byte[] buffer = new byte[pieceSize]; using (Stream stream = await file.OpenAsync(PCLStorage.FileAccess.Read)) { stream.Seek((int)(id * Constants.FileSliceMaxLength), SeekOrigin.Begin); await stream.ReadAsync(buffer, 0, pieceSize); } return(buffer); } catch (Exception ex) { Debug.WriteLine("Exception in GetFileSlice(): " + ex.Message); return("Invalid Request".Select(c => (byte)c).ToArray()); } }