public static UploadSession GenerateUploadSession(string url_edit, FileType fileType) { if (Sessions == null) { Sessions = new List <UploadSession>(); } bool exist = true; Guid token; Guid validToken; do { token = Guid.NewGuid(); validToken = Guid.NewGuid(); bool vatoken = Sessions.FirstOrDefault(fs => fs.Token == token) == null; bool vavatoken = Sessions.FirstOrDefault(fs => fs.ValidToken == validToken) == null; if (vatoken && vavatoken) { exist = false; } } while (exist); UploadSession session = new UploadSession(token, DateTime.Now, validToken, url_edit, fileType); Sessions.Add(session); return(session); }
private IList <CorruptedPart> FindCorruptedParts(UploadSession session, IEnumerable <BlockInfo> uploadedBlockList) { var corruptedParts = new List <CorruptedPart>(); var uploadedBlocks = uploadedBlockList.ToDictionary(key => key.Id, val => val); foreach (var currentSessionPart in session.Parts) { if (!uploadedBlocks.TryGetValue(currentSessionPart.Id, out var uploadedBlock)) { corruptedParts.Add(new CorruptedPart { Id = currentSessionPart.Id, Offset = currentSessionPart.Offset, ExpectedSizeInBytes = currentSessionPart.SizeInBytes, ActualSizeInBytes = 0 }); } else { if (uploadedBlock.SizeInBytes != currentSessionPart.SizeInBytes) { corruptedParts.Add(new CorruptedPart { Id = currentSessionPart.Id, Offset = currentSessionPart.Offset, ExpectedSizeInBytes = currentSessionPart.SizeInBytes, ActualSizeInBytes = uploadedBlock.SizeInBytes }); } } } return(corruptedParts); }
public ActionResult Upload(UploadSession session, string spaceId) { _logger.Info("DocumentController 添加文档"); try { var files = session.UploadedFiles; var userId = _contextService.UserId; var userName = _contextService.NickName; var depId = _contextService.DepId; var space = _spaceService.GetSpace(spaceId); _logger.Info(string.Format("Add Document, spaceId:[{0}];spaceSeqNo:[{1}], spaceName:[{2}], userId:[{3}];userName:[{4}]。", space.Id, space.SpaceSeqNo, space.SpaceName, userId, userName)); var documents = new List <DocumentObject>(); foreach (var file in files) { var document = DocumentBuilder.Build(file, space.Id.ToString(), space.SpaceSeqNo, space.SpaceName, userId, userName, depId, Visible.Public); documents.Add(document); } var contracts = _documentService.Add(documents); return(Json(new UploadViewModel(ErrorMessages.Success, ErrorMessages.GetErrorMessages(ErrorMessages.Success), contracts))); } catch (Exception ex) { _logger.Error(ex.StackTrace); _logger.Error(ex.Message); return(Json(new UploadViewModel(ErrorMessages.UploadFailed, ex.Message))); } }
public void should_return_completed_when_the_last_chunk_is_added() { var session = new UploadSession(); var completed = session.AddChunk(_filename, _filepath + "BodyPart_5e9c51db-180a-47c6-864c-256474957298", 1, 1); completed.Should().BeTrue(); }
public async Task UploadFiles_GivenValidArgsAndSessionStarted_DoesNotThrowException() { // Arrange string sessionId = "780352d9-ed37-47de-b03d-d47327421df9"; string file01Id = "d9c2e134-9687-4c2f-a639-4080d4bf15d9"; string file02Id = "8a4a155b-92b9-48f9-8d5c-d5f9c495ea53"; UploadFilesCommand command = new UploadFilesCommand(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), new CommandIssuer(Guid.NewGuid().ToString(), Guid.NewGuid().ToString()), sessionId, new List <FileData>() { new FileData(file01Id, new MemoryStream(Encoding.UTF8.GetBytes("testcontent01"))), new FileData(file02Id, new MemoryStream(Encoding.UTF8.GetBytes("testcontent02"))), }); var applicationService = this.Fixture.Kernel.Get <IFileStorageCommandApplicationService>(); var uploadSessionRepository = this.Fixture.Kernel.Get <IUploadSessionRepository>(); // Act await applicationService.UploadFilesAsync(command); // Assert UploadSession session = await uploadSessionRepository.GetByIdAsync(new Guid(sessionId)); Assert.NotNull(session); Assert.True(session.IsFileUploaded(new Guid(file01Id))); Assert.True(session.IsFileUploaded(new Guid(file02Id))); }
public async Task <ODItem> UploadFileStream() { long currentPosition = 0; ODItem uploadedItem = null; byte[] fragmentBuffer = new byte[UploadOptions.FragmentSize]; while (currentPosition < DataSource.Length) { long endPosition = currentPosition + UploadOptions.FragmentSize; if (endPosition > DataSource.Length) { endPosition = DataSource.Length; } await DataSource.ReadAsync(fragmentBuffer, 0, (int)Math.Max(0, endPosition - currentPosition)); var response = await ExecuteUploadFragment(currentPosition, Math.Max(0, endPosition - 1), fragmentBuffer); if (response is ODUploadSession) { UploadSession.ApplySessionDelta((ODUploadSession)response); } else if (response is ODItem) { uploadedItem = (ODItem)response; } UpdateProgress(endPosition); currentPosition = endPosition; } return(uploadedItem); }
public async Task StartUploadSession_GivenValidArgs_DoesNotThrowException() { var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); Console.WriteLine(config.FilePath); // Arrange string sessionId = "16715015-d920-4998-87fa-9344145c901c"; StartUploadSessionCommand command = new StartUploadSessionCommand(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), new CommandIssuer(Guid.NewGuid().ToString(), Guid.NewGuid().ToString()), sessionId, new OwnerIdData(Guid.NewGuid().ToString(), Guid.NewGuid().ToString()), new List <FileDescriptionData>() { new FileDescriptionData(Guid.NewGuid().ToString(), "testFile.pdf", "application/pdf", 1048576), new FileDescriptionData(Guid.NewGuid().ToString(), "testFile2.pdf", "application/msword", 1048576) }, true); var applicationService = this.Fixture.Kernel.Get <IFileStorageCommandApplicationService>(); var uploadSessionRepository = this.Fixture.Kernel.Get <IUploadSessionRepository>(); // Act await applicationService.StartUploadSessionAsync(command); // Assert UploadSession session = await uploadSessionRepository.GetByIdAsync(new Guid(sessionId)); Assert.NotNull(session); Assert.Equal(sessionId, session.Id.ToString()); }
void context_PostRequestHandlerExecute(object sender, EventArgs e) { HttpContext context = HttpContext.Current; UploadSession session = SlickUploadContext.CurrentUploadSessionInternal; if (session != null && session.State != UploadState.Uploading) { SlickUploadContext.CommitSession(session); } ComponentHelper.EnsureScriptsRendered(); // We're set to use session state, but we didn't create a cookie. Ensure we do. if (HttpContext.Current.Items["EnsureSessionCreated"] != null) { HttpCookie cookie = context.Response.Cookies[_sessionCookieName]; if (cookie == null) { cookie = new HttpCookie(_sessionCookieName, context.Session.SessionID) { Path = "/", HttpOnly = true } } ; else { cookie.Value = context.Session.SessionID; } context.Items["SlickUploadSessionStateFixCookie"] = cookie; } }
private DashboardViewModel MapUploadSessionToViewModel(UploadSession uploadSession) { var dashboardVM = new DashboardViewModel(); dashboardVM.SessionNumber = uploadSession.SessionNumber; dashboardVM.UploadSessionDate = uploadSession.Date; dashboardVM.Location = uploadSession.Location; dashboardVM.Description = uploadSession.Description; dashboardVM.UsesQuic = uploadSession.UsesQuic; dashboardVM.Testmode = ComposeTestmodeText(uploadSession); dashboardVM.AverageNewSessionToRequestIssuance = uploadSession.AverageNewSessionToRequestIssuance; dashboardVM.AverageRespondToSuccess = uploadSession.AverageRespondToSuccess; dashboardVM.AverageNewSessionToServerLog = uploadSession.AverageNewSessionToServerLog; dashboardVM.AverageServerLogToRequestIssuance = uploadSession.AverageServerLogToRequestIssuance; dashboardVM.AverageRespondToServerLog = uploadSession.AverageRespondToServerLog; dashboardVM.AverageServerLogToSuccess = uploadSession.AverageServerLogToSuccess; // Project calculated fields of IrmaSessions. var irmaSessionSelection = uploadSession.IrmaSessions.Select (irmaSession => new IrmaSessionViewModel() { AppSessionId = irmaSession.AppSessionId, StartTime = irmaSession.Timestamp.ToLongTimeString(), Location = irmaSession.Location, NewSessionToRequestIssuanceDelta = irmaSession.NewSessionToRequestIssuanceDelta, RespondToSuccessDelta = irmaSession.RespondToSuccessDelta, NewSessionToServerLogDelta = irmaSession.NewSessionToServerLogDelta, ServerLogToRequestIssuanceDelta = irmaSession.ServerLogToRequestIssuanceDelta, RespondToServerLogDelta = irmaSession.RespondToServerLogDelta, ServerLogToSuccessDelta = irmaSession.ServerLogToSuccessDelta, }); // Filter the projection on criterium: app delta should be more or equal as sum of the corresponding app-server deltas. var filteredIrmaSessionProjection = irmaSessionSelection .Where(x => x.NewSessionToRequestIssuanceDelta >= 0 && (x.NewSessionToServerLogDelta + x.ServerLogToRequestIssuanceDelta <= x.NewSessionToRequestIssuanceDelta) && x.RespondToSuccessDelta >= 0 && (x.RespondToServerLogDelta + x.ServerLogToSuccessDelta <= x.RespondToSuccessDelta) ); if (!filteredIrmaSessionProjection.Any()) { // app and server logs calculations do not match! show them anyway with warning: corrupt data. dashboardVM.IrmaSessions = irmaSessionSelection .OrderBy(sessions => sessions.StartTime) .ToList(); dashboardVM.ErrorMessage = "The app and server logs calculations do not match, this should not happen! Showing them for reference"; } else { dashboardVM.IrmaSessions = filteredIrmaSessionProjection .OrderBy(sessions => sessions.StartTime) .ToList(); dashboardVM.InvalidTestAmount = irmaSessionSelection.Count() - dashboardVM.ValidTestAmount; } return(dashboardVM); }
private async Task FinishUploadSessionAsync(UploadSession session, List <BlockInfo> uploadedBlockList, CancellationToken cancellationToken) { await Task.WhenAll( _blobStorage.CommitBlockListAsync(session.Id, uploadedBlockList.Select(x => x.Id), cancellationToken), _sessions.CompleteSessionAsync(session.Id, cancellationToken)); }
public void EndPart(bool isLast, bool isComplete) { //SimpleLogger.Log("Part complete", _uploadStatus.UploadId); if (_currentStream != null) { UploadSession session = SlickUploadContext.SessionStorageProvider.GetSession(_request.UploadSessionId); if (session != null && session.State == UploadState.Uploading && (session.FailedRequests == null || Array.IndexOf <string>(session.FailedRequests, _request.UploadRequestId) == -1)) { _currentFile.ContentLength = _fileLength; _request.UploadStreamProvider.CloseWriteStream(_currentFile, _currentStream, isComplete); _currentStream = null; _currentFile = null; } else { CancelParse(); if (session != null && session.ErrorType == UploadErrorType.Cancelled) { throw new UploadCancelledException(); } else { throw new UploadDisconnectedException(); } } } else { _textParts.Append("\r\n"); if (_request.Data.Count == 0) { string data = _httpRequest.Form["kw_uploadData"]; if (string.IsNullOrEmpty(data)) { data = _httpRequest.Form["X-SlickUpload-Data"]; } if (!string.IsNullOrEmpty(data)) { MimeHelper.ParseQueryStringToDictionary(data, _request.Data); } } } if (isLast) { if (_textParts.Length > 0) { // Write out the boundary end _textParts.Append(_httpRequest.ContentEncoding.GetString(_boundary) + "--\r\n\r\n"); } } }
private static async Task <DriveItem> UploadlargeFile(SharepointIds sp, string path) { DriveItem uploadedFile = null; using (FileStream fileStream = new FileStream(largeFilePath, FileMode.Open)) { UploadSession uploadSession = await graphClient.Sites["root"] .Drive.Root.ItemWithPath($"{path}/LargeFile.txt") .CreateUploadSession().Request() .PostAsync(); if (uploadSession != null) { // Chunk size must be divisible by 320KiB, our chunk size will be slightly more than 1MB int maxSizeChunk = (320 * 1024) * 4; ChunkedUploadProvider uploadProvider = new ChunkedUploadProvider(uploadSession, graphClient, fileStream, maxSizeChunk); var chunkRequests = uploadProvider.GetUploadChunkRequests(); var exceptions = new List <Exception>(); var readBuffer = new byte[maxSizeChunk]; foreach (var request in chunkRequests) { var result = await uploadProvider.GetChunkRequestResponseAsync(request, readBuffer, exceptions); if (result.UploadSucceeded) { uploadedFile = result.ItemResponse; } } } } return(uploadedFile); }
/// <inheritdoc /> public override IEnumerable <UploadSession> GetStaleSessions(DateTime staleBefore) { List <UploadSession> sessions = new List <UploadSession>(); string sql; sql = "SELECT " + _statusField + " FROM " + _table + " AS kw_sessions WHERE " + _requestIdField + " IS NULL AND (SELECT MAX(" + _lastUpdatedField + ") FROM " + _table + " AS kw_status WHERE kw_status." + _sessionIdField + "=kw_sessions." + _sessionIdField + ") < " + _lastUpdatedParameterName; using (IDbConnection cn = CreateConnection(_connectionString)) using (IDbCommand cmd = cn.CreateCommand()) { cmd.CommandText = sql; AddParameter(cmd, _lastUpdatedParameterName, DbType.DateTime, staleBefore); cn.Open(); using (IDataReader rd = cmd.ExecuteReader(CommandBehavior.SingleResult)) { while (rd.Read()) { sessions.Add(UploadSession.Deserialize(rd.GetString(0))); } } } return(sessions); }
public void should_add_chunk_to_session() { var session = new UploadSession(); session.AddChunk(_filename, "BodyPart_5e9c51db-180a-47c6-864c-256474957298", 1, 4); session.Chunks.Count.ShouldBeEquivalentTo(1); }
// Uploads a large file to the current user's root directory. public async Task <List <ResultsItem> > UploadLargeFile() { List <ResultsItem> items = new List <ResultsItem>(); using (Stream fileStream = System.IO.File.OpenRead(HostingEnvironment.MapPath("/Content/LargeFileUploadResource.bmp"))) { // Create the upload session. The access token is no longer required as you have session established for the upload. // POST /v1.0/drive/root:/UploadLargeFile.bmp:/microsoft.graph.createUploadSession UploadSession uploadSession = await graphClient.Me.Drive.Root.ItemWithPath("LargeFileUploadResource.bmp").CreateUploadSession().Request(requestOptions) .WithUserAccount(ClaimsPrincipal.Current.ToGraphUserAccount()) .PostAsync(); int maxChunkSize = 320 * 1024; // 320 KB - Change this to your chunk size. 5MB is the default. ChunkedUploadProvider provider = new ChunkedUploadProvider(uploadSession, graphClient, fileStream, maxChunkSize); // Set up the chunk request necessities. IEnumerable <UploadChunkRequest> chunkRequests = provider.GetUploadChunkRequests(requestOptions); byte[] readBuffer = new byte[maxChunkSize]; List <Exception> trackedExceptions = new List <Exception>(); DriveItem uploadedFile = null; // Upload the chunks. foreach (var request in chunkRequests) { request.WithUserAccount(ClaimsPrincipal.Current.ToGraphUserAccount()); // Do your updates here: update progress bar, etc. // ... // Send chunk request UploadChunkResult result = await provider.GetChunkRequestResponseAsync(request, readBuffer, trackedExceptions); if (result.UploadSucceeded) { uploadedFile = result.ItemResponse; // Get file properties. items.Add(new ResultsItem { Display = uploadedFile.Name, Id = uploadedFile.Id, Properties = new Dictionary <string, object> { { Resource.Prop_Created, uploadedFile.CreatedDateTime.Value.ToLocalTime() }, { Resource.Prop_Url, uploadedFile.WebUrl }, { Resource.Prop_Id, uploadedFile.Id } } }); } } // Check that upload succeeded. if (uploadedFile == null) { // Retry the upload // ... } } return(items); }
public Task <Video> UploadFile(UploadSession sess, Stream dataStream, IProgress <double> progress = null, CancellationToken cancelToken = default(CancellationToken)) { session = sess; main_file = dataStream; return(this.UploadFile(null, new CancellationToken())); }
/// <inheritdoc/> public async Task <IOneDriveStorageFile> UploadFileAsync(string desiredName, IRandomAccessStream content, CreationCollisionOption options = CreationCollisionOption.FailIfExists, int maxChunkSize = -1) { int currentChunkSize = maxChunkSize < 0 ? OneDriveUploadConstants.DefaultMaxChunkSizeForUploadSession : maxChunkSize; if (currentChunkSize % OneDriveUploadConstants.RequiredChunkSizeIncrementForUploadSession != 0) { throw new ArgumentException("Max chunk size must be a multiple of 320 KiB", nameof(maxChunkSize)); } if (string.IsNullOrEmpty(desiredName)) { throw new ArgumentNullException(nameof(desiredName)); } if (content == null) { throw new ArgumentNullException(nameof(content)); } var uploadSessionUri = $"{Provider.BaseUrl}/drive/items/{OneDriveItem.Id}:/{desiredName}:/createUploadSession"; var conflictBehavior = new OneDriveItemConflictBehavior { Item = new OneDriveConflictItem { ConflictBehavior = OneDriveHelper.TransformCollisionOptionToConflictBehavior(options.ToString()) } }; var jsonConflictBehavior = JsonConvert.SerializeObject(conflictBehavior); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, uploadSessionUri) { Content = new StringContent(jsonConflictBehavior, Encoding.UTF8, "application/json") }; await Provider.AuthenticationProvider.AuthenticateRequestAsync(request).ConfigureAwait(false); var response = await Provider.HttpProvider.SendAsync(request).ConfigureAwait(false); if (!response.IsSuccessStatusCode) { throw new ServiceException(new Error { Message = "Could not create an UploadSession", Code = "NoUploadSession", ThrowSite = "UWP Community Toolkit" }); } IsUploadCompleted = false; var jsonData = await response.Content.ReadAsStringAsync().ConfigureAwait(false); _uploadSession = JsonConvert.DeserializeObject <UploadSession>(jsonData); var streamToUpload = content.AsStreamForRead(); _uploadProvider = new ChunkedUploadProvider(_uploadSession, Provider, streamToUpload, maxChunkSize); var uploadedItem = await _uploadProvider.UploadAsync().ConfigureAwait(false); IsUploadCompleted = true; return(InitializeOneDriveStorageFile(uploadedItem)); }
/// <summary> /// Uploads a new file into the folder id specificied /// </summary> /// <param name="folderId"></param> /// <returns></returns> public async Task UploadFileAsync(string folderId) { // Check if session is set if (AuthenticationService == null) { throw new InvalidOperationException($"No {nameof(AuthenticationService)} has been specified"); } // Open the picker for file selection var picker = new FileOpenPicker { SuggestedStartLocation = PickerLocationId.Downloads, }; // User can select any file picker.FileTypeFilter.Add("*"); StorageFile storageFile = await picker.PickSingleFileAsync(); // storageFile is null if no file has been selected if (storageFile != null) { // Create the graph request builder for the drive IDriveRequestBuilder driveRequest = AuthenticationService.GraphClient.Me.Drive; // If folder id is null, the request refers to the root folder IDriveItemRequestBuilder driveItemsRequest; if (folderId == null) { driveItemsRequest = driveRequest.Root; } else { driveItemsRequest = driveRequest.Items[folderId]; } try { // Create an upload session for a file with the same name of the user selected file UploadSession session = await driveItemsRequest .ItemWithPath(storageFile.Name) .CreateUploadSession() .Request() .PostAsync(); // Add a new upload item at the beginning var item = new OneDriveFileProgress(storageFile.Name); _progressItems.Insert(0, item); // Start the upload process await item.UploadFileAsync(AuthenticationService, storageFile, session); } catch (Exception ex) { Error?.Invoke(this, ex); } } }
public Guid StartNewSession() { var correlationId = Guid.NewGuid(); var session = new UploadSession(); _uploadSessions.TryAdd(correlationId.ToString(), session); return(correlationId); }
public async Task <IUploadSession> CreateAsync( string uploadSessionIdentifier, string fileIdentifier, string fileName) { if (uploadSessionIdentifier == null) { throw new ArgumentNullException("uploadSessionIdentifier"); } if (string.IsNullOrWhiteSpace(uploadSessionIdentifier)) { throw new ArgumentException( "fileIdentifier must not be empty nor whitespace.", "uploadSessionIdentifier"); } if (fileIdentifier == null) { throw new ArgumentNullException("fileIdentifier"); } if (string.IsNullOrWhiteSpace(fileIdentifier)) { throw new ArgumentOutOfRangeException("fileIdentifier", "fileIdentifier must not be empty nor whitespace."); } var uploadSession = new UploadSession { Id = uploadSessionIdentifier, FileIdentifier = fileIdentifier, FileName = fileName, ExpirationDateTime = DateTimeOffset.UtcNow.Add(_uploadSessionTimeout) }; var uploadSessionDirectory = GetUploadSessionDirectoryInfo(uploadSessionIdentifier); var uploadSessionMetadataFile = GetUploadSessionMetadataFileInfo(uploadSessionIdentifier); if (uploadSessionDirectory.Exists) { throw ExceptionHelper.ResourceAlreadyExists(uploadSessionIdentifier); } try { uploadSessionDirectory.Create(); await SaveUploadSessionMetadataAsync(uploadSession); return(uploadSession); } catch (Exception exception) { uploadSessionDirectory.Delete(); throw ExceptionHelper.OtherError(exception); } }
public static UploadSession CalculateAverages(UploadSession uploadSession, List <IrmaSession> irmaSessions) { uploadSession.AverageNewSessionToRequestIssuance = RoundToThreeDecimals(irmaSessions.Select(i => i.NewSessionToRequestIssuanceDelta).Average()); uploadSession.AverageRespondToSuccess = RoundToThreeDecimals(irmaSessions.Select(i => i.RespondToSuccessDelta).Average()); uploadSession.AverageNewSessionToServerLog = RoundToThreeDecimals(irmaSessions.Select(i => i.NewSessionToServerLogDelta).Average()); uploadSession.AverageServerLogToRequestIssuance = RoundToThreeDecimals(irmaSessions.Select(i => i.ServerLogToRequestIssuanceDelta).Average()); uploadSession.AverageRespondToServerLog = RoundToThreeDecimals(irmaSessions.Select(i => i.RespondToServerLogDelta).Average()); uploadSession.AverageServerLogToSuccess = RoundToThreeDecimals(irmaSessions.Select(i => i.ServerLogToSuccessDelta).Average()); return(uploadSession); }
// Upload attachment private static UploadResult <AttachmentItem> UploadAttachment(UploadSession uploadSession, FileStream attachmentStream) { var mailboxHelper = new MailboxHelper(_httpClient); var uploadResult = mailboxHelper.UploadAttachment(uploadSession, attachmentStream).Result; Console.WriteLine("Upload Succeeded: " + uploadResult.UploadSucceeded); return(uploadResult); }
/// <summary> /// Serializes the object to JSON. /// </summary> /// <param name="writer">The <see cref="T: Newtonsoft.Json.JsonWriter" /> to write to.</param> /// <param name="obj">The object to serialize to JSON.</param> internal static void Serialize(JsonWriter writer, UploadSession obj) { // Required properties are always serialized, optional properties are serialized when not null. writer.WriteStartObject(); if (obj.UploadSessions != null) { writer.WriteEnumerableProperty(obj.UploadSessions, "UploadSessions", UploadSessionInfoConverter.Serialize); } writer.WriteEndObject(); }
public static async Task <DriveItem> UploadFileAsync(Stream fileStream, string fileName, bool isLargeFile, string siteUrl) { var graphClient = GetAuthenticatedClient(); DriveItem uploadedFile = null; siteUrl = string.IsNullOrEmpty(siteUrl.Trim())? "/sites/web01" : siteUrl; //var hostname = "m365x130314.sharepoint.com"; try { var site = await graphClient.Sites.GetByPath(siteUrl, hostname).Request().GetAsync(); //var lists = await graphClient.Sites.GetByPath(siteUrl, hostname).Lists.Request().GetAsync(); //var listId = lists.First(p => p.Name == "Documents").Id; if (!isLargeFile) { uploadedFile = await graphClient.Sites[site.Id].Drive.Root.ItemWithPath(fileName).Content.Request().PutAsync <DriveItem>(fileStream); } //uploadedFile = await graphClient.Sites.GetByPath(siteUrl, hostname).Lists[listId].Drive.Root.ItemWithPath(fileName).Content.Request().PutAsync<DriveItem>(fileStream); //uploadedFile = await graphClient.Sites[hostname + siteUrl].Drive.Root.ItemWithPath(fileName).Content.Request().PutAsync<DriveItem>(fileStream); else { UploadSession uploadSession = null; //uploadSession = await graphClient.Sites["root"].SiteWithPath("/sites/team01").Drive.Root.ItemWithPath(fileName).CreateUploadSession().Request().PostAsync(); uploadSession = await graphClient.Sites[site.Id].Drive.Root.ItemWithPath(fileName).CreateUploadSession().Request().PostAsync(); if (uploadSession != null) { // Chunk size must be divisible by 320KiB, our chunk size will be slightly more than 1MB int maxSizeChunk = (320 * 1024) * 16; ChunkedUploadProvider uploadProvider = new ChunkedUploadProvider(uploadSession, graphClient, fileStream, maxSizeChunk); var chunkRequests = uploadProvider.GetUploadChunkRequests(); var exceptions = new List <Exception>(); var readBuffer = new byte[maxSizeChunk]; foreach (var request in chunkRequests) { var result = await uploadProvider.GetChunkRequestResponseAsync(request, readBuffer, exceptions); if (result.UploadSucceeded) { uploadedFile = result.ItemResponse; } } } } } catch (Exception ex) { throw ex; } return(uploadedFile); }
private string ComposeTestmodeText(UploadSession uploadSession) { string text = ""; text += uploadSession.IsWiFi ? "WiFi, " : ""; text += uploadSession.IsMostly3G ? "3G, " : ""; text += uploadSession.IsMostly4G ? "4G, " : ""; text += uploadSession.IsStationary ? "stationary" : "moving"; text = text.Trim(',').Trim(); return(text); }
/// <summary> /// Get the status of the session. Stores returned session internally. /// Updates internal list of ranges remaining to be uploaded (according to the server). /// </summary> /// <returns>UploadSession returned by the server.</returns> public virtual async Task <UploadSession> UpdateSessionStatusAsync() { var request = new UploadSessionRequest(this.Session, this.client, null); var newSession = await request.GetAsync(); var newRangesRemaining = this.GetRangesRemaining(newSession); this.rangesRemaining = newRangesRemaining; newSession.UploadUrl = this.Session.UploadUrl; // Sometimes the UploadUrl is not returned this.Session = newSession; return(newSession); }
public static async Task <DriveItem> UploadDocumentAsync(IDriveItemRequestBuilder driveItemRequestBuilder, string filename, Stream fileStream, string conflictBehaviour = ConflictBehaviour.Rename) { UploadSession uploadSession = null; try { var uploadProps = new DriveItemUploadableProperties { ODataType = null, AdditionalData = new Dictionary <string, object> { ["@microsoft.graph.conflictBehavior"] = conflictBehaviour } }; uploadSession = await driveItemRequestBuilder .ItemWithPath(filename) .CreateUploadSession(uploadProps) .Request() .PostAsync(); } catch (ServiceException ex) { throw new Exception("An error occured while creating upload session.", ex); } if (uploadSession == null) { throw new Exception("Upload session is null."); } try { // Performs upload, slice by slice int maxSliceSize = 5 * 1024 * 1024; //5MB var fileUploadTask = new LargeFileUploadTask <DriveItem>(uploadSession, fileStream, maxSliceSize); var uploadResult = await fileUploadTask.UploadAsync(); if (!uploadResult.UploadSucceeded) { throw new Exception("File upload failed!"); } else { return(uploadResult.ItemResponse); } } catch (Exception ex) { throw new Exception("An error occurred while trying to upload document.", ex); } }
private static async Task <UploadSession> ReadUploadSessionAsync(FileInfo uploadSessionFile) { UploadSession uploadSession = null; using (var reader = new StreamReader(uploadSessionFile.FullName, Encoding.UTF8)) { string metadataString = await reader.ReadToEndAsync(); uploadSession = JsonConvert.DeserializeObject <UploadSession>(metadataString); } return(uploadSession); }
// Upload attachments public async Task <UploadResult <AttachmentItem> > UploadAttachment(UploadSession uploadSession, FileStream attachmentStream) { int fileSlice = 320 * 1024; var fileUploadTask = new LargeFileUploadTask <AttachmentItem>(uploadSession, attachmentStream, fileSlice); IProgress <long> progress = new Progress <long>(progress => { Console.WriteLine($"Uploaded {progress} bytes of {attachmentStream.Length} bytes"); }); // Upload the file return(await fileUploadTask.UploadAsync(progress)); }
/// <inheritdoc /> public override UploadSession GetSession(string uploadSessionId) { string data = GetData(uploadSessionId, null); if (!string.IsNullOrEmpty(data)) { return(UploadSession.Deserialize(data)); } else { return(null); } }
/// <summary> /// Helps with resumable uploads. Generates chunk requests based on <paramref name="session"/> /// information, and can control uploading of requests using <paramref name="client"/> /// </summary> /// <param name="session">Session information.</param> /// <param name="client">Client used to upload chunks.</param> /// <param name="uploadStream">Readable, seekable stream to be uploaded. Length of session is determined via uploadStream.Length</param> /// <param name="maxChunkSize">Max size of each chunk to be uploaded. Multiple of 320 KiB (320 * 1024) is required. /// If less than 0, default value of 5 MiB is used. .</param> public ChunkedUploadProvider(UploadSession session, IBaseClient client, Stream uploadStream, int maxChunkSize = -1) { if (!uploadStream.CanRead || !uploadStream.CanSeek) { throw new ArgumentException("Must provide stream that can read and seek"); } this.Session = session; this.client = client; this.uploadStream = uploadStream; this.rangesRemaining = this.GetRangesRemaining(session); this.maxChunkSize = maxChunkSize < 0 ? DefaultMaxChunkSize : maxChunkSize; if (this.maxChunkSize % RequiredChunkSizeIncrement != 0) { throw new ArgumentException("Max chunk size must be a multiple of 320 KiB", nameof(maxChunkSize)); } }
/// <inheritdoc /> public abstract void SaveSession(UploadSession session, bool isCreate);
void OnUploadComplete() { _session = SlickUploadContext.CurrentUploadSession; if (_session != null && UploadComplete != null && (string.IsNullOrEmpty(_session.SourceUploadConnectorId) || ClientID == _session.SourceUploadConnectorId)) { string key = "kw_HasOnCompleteFired" + _session.UploadSessionId; bool hasOnCompleteFired = Context.Items[key] is bool ? (bool)Context.Items[key] : false; if (!hasOnCompleteFired) { Context.Items[key] = true; UploadComplete(this, new UploadSessionEventArgs(_session)); ComponentHelper.EnsureParentUpdatePanelUpdated(this); } } }
/// <inheritdoc /> public override void SaveSession(UploadSession session, bool isCreate) { InsertOrUpdate(session.UploadSessionId, null, session.Serialize(), DateTime.Now, isCreate); }
static void SaveSession(HttpSessionState state, UploadSession session) { state[GetUploadSessionKey(session.UploadSessionId)] = GetSerializedString(session); }
internal static void RenderProgress(HttpContext context, UploadSession session) { StringBuilder sb = new StringBuilder(); sb.Append('{'); if (session != null) { JsonHelper.AppendJson(sb, "uploadSessionId", session.UploadSessionId); JsonHelper.AppendJson(sb, "state", session.State.ToString()); foreach (KeyValuePair<string, string> pair in session.ProcessingStatus) JsonHelper.AppendJson(sb, pair.Key, pair.Value); } sb.Append('}'); WriteResponse(context, sb.ToString(), session != null ? session.UploadProfile : null); }
/// <inheritdoc /> public void ProcessRequest(HttpContext context) { try { string uploadRequestId = context.Request.QueryString["uploadRequestId"]; string uploadSessionId = context.Request.QueryString["uploadSessionId"]; //if (string.IsNullOrEmpty(uploadRequestId)) // throw new HttpException(500, "uploadRequestId parameter is required."); if (string.IsNullOrEmpty(uploadSessionId)) throw new HttpException(500, "uploadSessionId parameter is required."); if (!string.IsNullOrEmpty(uploadRequestId)) { UploadRequest request = SlickUploadContext.SessionStorageProvider.GetRequest(uploadSessionId, uploadRequestId); if (request == null && string.Equals(context.Request.QueryString["isLastRetry"], "true", StringComparison.InvariantCultureIgnoreCase)) { string uploadProfile = context.Request.QueryString["uploadProfile"]; UploadSession session = SlickUploadContext.SessionStorageProvider.GetSession(uploadSessionId); if (session == null) { session = new UploadSession(uploadSessionId, uploadProfile); session.State = UploadState.Uploading; SlickUploadContext.SessionStorageProvider.SaveSession(session, true); } request = new UploadRequest(uploadSessionId, uploadRequestId, -1, uploadProfile); request.State = UploadState.Error; request.ErrorType = UploadErrorType.RequestNotRecieved; //request._uploadedFilesInternal.Add(new UploadedFile(context.Request.QueryString["name"], null, null, request, null)); SlickUploadContext.SessionStorageProvider.SaveRequest(request, true); } RenderProgress(context, request); } else RenderProgress(context, SlickUploadContext.SessionStorageProvider.GetSession(uploadSessionId)); } catch (Exception ex) { UploadProfileElement profileElement = SlickUploadContext.Config.UploadProfiles.GetUploadProfileElement(context.Request.QueryString["uploadProfile"], false); StringBuilder sb = new StringBuilder(); sb.Append('{'); JsonHelper.AppendJson(sb, "progressException", ex.ToString()); sb.Append('}'); WriteResponse(context, sb.ToString(), profileElement); } }
internal List<Tuple<long, long>> GetRangesRemaining(UploadSession session) { // nextExpectedRanges: https://dev.onedrive.com/items/upload_large_files.htm // Sample: ["12345-55232","77829-99375"] // Also, second number in range can be blank, which means 'until the end' var newRangesRemaining = new List<Tuple<long, long>>(); foreach (var range in session.NextExpectedRanges) { var rangeSpecifiers = range.Split('-'); newRangesRemaining.Add(new Tuple<long, long>(long.Parse(rangeSpecifiers[0]), string.IsNullOrEmpty(rangeSpecifiers[1]) ? this.totalUploadLength - 1 : long.Parse(rangeSpecifiers[1]))); } return newRangesRemaining; }
void context_PreRequestHandlerExecute(object sender, EventArgs e) { HttpContext context = HttpContext.Current; //System.Diagnostics.Debug.WriteLine(context.Request.Url.ToString() + " - " + (context.Request.Cookies.Count > 0 ? context.Request.Cookies[0].Value : "null")); if (IsUploadRequest(context.Request)) { if (context.Trace != null && context.Trace.IsEnabled) throw new HttpException("Trace must be disabled for SlickUpload to intercept upload requests."); if ((DateTime.Now - _lastCleanupDate).TotalSeconds > SlickUploadContext.Config.SessionStorageProvider.StaleTimeout) { lock (_cleanupThreadLock) { if ((DateTime.Now - _lastCleanupDate).TotalSeconds > SlickUploadContext.Config.SessionStorageProvider.StaleTimeout) { _lastCleanupDate = DateTime.Now; Thread cleanupThread = new Thread(CleanupThread); cleanupThread.Start(); } } } string uploadSessionId = context.Request.QueryString["uploadSessionId"]; // Generate an uploadSessionId if none was specified if (string.IsNullOrEmpty(uploadSessionId)) uploadSessionId = Guid.NewGuid().ToString(); UploadSession session = SlickUploadContext.SessionStorageProvider.GetSession(uploadSessionId); if (session == null) { session = new UploadSession(uploadSessionId, context.Request.QueryString["uploadProfile"]); session.State = UploadState.Uploading; SlickUploadContext.SessionStorageProvider.SaveSession(session, true); } SlickUploadContext.CurrentUploadSessionInternal = session; string uploadRequestId = context.Request.QueryString["uploadRequestId"]; // Generate an uploadRequestId if none was specified if (string.IsNullOrEmpty(uploadRequestId)) uploadRequestId = Guid.NewGuid().ToString(); UploadHttpRequest httpRequest = new UploadHttpRequest(context); UploadRequest request = new UploadRequest(uploadSessionId, uploadRequestId, httpRequest.ContentLength, context.Request.QueryString["uploadProfile"]); bool isCalculateSize = (context.Request.QueryString["handlerType"] == "calculatesize"); try { SlickUploadContext.CurrentUploadRequest = request; if (httpRequest.ContentLength > request.UploadProfile.MaxRequestLengthBytes || (httpRequest.ContentLength < 0 && request.UploadProfile.MaxRequestLength > 2097152)) { //SimpleLogger.Log("Request too big... aborting"); request.ErrorType = UploadErrorType.MaxRequestLengthExceeded; context.ApplicationInstance.CompleteRequest(); } if (request.ErrorType == UploadErrorType.None && !(request.UploadFilter == null || request.UploadFilter.ShouldHandleRequest(httpRequest))) { request.ErrorType = UploadErrorType.UploadFilter; } if (request.ErrorType != UploadErrorType.None) request.State = UploadState.Error; else if (isCalculateSize) request.State = UploadState.Initializing; else request.State = UploadState.Uploading; } catch (Exception ex) { request.State = UploadState.Error; request.ErrorType = UploadErrorType.Other; request.Error = ex; throw; } finally { bool hasExistingRequest = false; try { hasExistingRequest = (SlickUploadContext.SessionStorageProvider.GetRequest(uploadSessionId, request.UploadRequestId) != null); } catch { } if (!hasExistingRequest) SlickUploadContext.InsertRequest(request); else SlickUploadContext.UpdateRequest(request, true); } if (request.ErrorType == UploadErrorType.None) { if (isCalculateSize) { httpRequest.Worker.CloseConnection(); context.ApplicationInstance.CompleteRequest(); return; } context.Server.ScriptTimeout = request.UploadProfile.ExecutionTimeout; MimeUploadHandler handler = null; try { handler = new MimeUploadHandler(httpRequest, request); handler.ProcessRequest(); // TODO: should we check the session's state here, and cancel? // TODO: maybe no, because MimeUploadHandler checks on part end and throws cancelled if it should request.State = UploadState.Complete; } catch (Exception ex) { if (handler != null) { try { handler.CancelParse(); } catch { // TODO: what do we do with this exception? } } request.State = UploadState.Error; if (ex is UploadDisconnectedException) { request.ErrorType = UploadErrorType.Disconnected; } else if (ex is UploadCancelledException) { request.ErrorType = UploadErrorType.Cancelled; } else { request.ErrorType = UploadErrorType.Other; request.Error = ex; } try { SlickUploadContext.CleanupRequest(request); } catch { // TODO: what do we do with this exception? } // If we were disconnected, let everything pass through. Otherwise, rethrow it. if (ex is UploadDisconnectedException || ex is UploadCancelledException) { httpRequest.Worker.CloseConnection(); context.ApplicationInstance.CompleteRequest(); } else throw; } finally { SlickUploadContext.UpdateRequest(request, true); } } } }
/// <summary> /// Get the status of the session. Stores returned session internally. /// Updates internal list of ranges remaining to be uploaded (according to the server). /// </summary> /// <returns>UploadSession returned by the server.</returns> public virtual async Task<UploadSession> UpdateSessionStatusAsync() { var request = new UploadSessionRequest(this.Session, this.client, null); var newSession = await request.GetAsync(); var newRangesRemaining = this.GetRangesRemaining(newSession); this.rangesRemaining = newRangesRemaining; newSession.UploadUrl = this.Session.UploadUrl; // Sometimes the UploadUrl is not returned this.Session = newSession; return newSession; }
/// <inheritdoc /> public override void SaveSession(UploadSession session, bool isCreate) { CallHandler("SaveSession", session); RegisterCleanupSessionIfStaleTimeout(session.UploadSessionId); }
/// <inheritdoc /> public override void SaveSession(UploadSession session, bool isCreate) { _provider.SaveSession(session, isCreate); }
/// <summary> /// Initializes a new instance of the <see cref="UploadSessionEventArgs" /> class. /// </summary> /// <param name="session">The <see cref="UploadSession" /> for this event.</param> internal UploadSessionEventArgs(UploadSession session) { _session = session; }
public UploadSessionRequest(UploadSession session, IBaseClient client, IEnumerable<Option> options) : base(session.UploadUrl, client, options) { this.session = session; }