private static async Task SaveUploadSessionMetadataAsync(IUploadSession uploadSession) { var uploadSessionFile = GetUploadSessionMetadataFileInfo(uploadSession.Id); var uploadSessionJson = JsonConvert.SerializeObject(uploadSession); var uploadSessionJsonBytes = Encoding.UTF8.GetBytes(uploadSessionJson); using (var fileStream = new FileStream(uploadSessionFile.FullName, FileMode.Create, FileAccess.Write)) { await fileStream.WriteAsync(uploadSessionJsonBytes, 0, uploadSessionJsonBytes.Length); } }
/// <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><see cref="IUploadSession"/>> returned by the server.</returns> public async Task <IUploadSession> UpdateSessionStatusAsync() { var request = new UploadSessionRequest(this.Session, this._client); var newSession = await request.GetAsync().ConfigureAwait(false); 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 UploadSession(IUploadSession uploadSession) { if (uploadSession == null) { throw new ArgumentNullException("uploadSession"); } Id = uploadSession.Id; FileIdentifier = uploadSession.FileIdentifier; FileName = uploadSession.FileName; FileMediaType = uploadSession.FileMediaType; FileSize = uploadSession.FileSize; ExpirationDateTime = uploadSession.ExpirationDateTime; }
private List <Tuple <long, long> > GetRangesRemaining(IUploadSession 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); }
public UploadSession( IUploadSession uploadSession, IFileMetadata uploadedFile) : this(uploadSession) { if (uploadedFile == null) { throw new ArgumentNullException("uploadedFile"); } FileIdentifier = uploadedFile.Id; FileName = uploadedFile.Name; FileMediaType = uploadedFile.MediaType; FileSize = uploadedFile.Size; UploadedFile = new File(uploadedFile); }
/// <summary> /// Task to help with resume able large file uploads. Generates slices based on <paramref name="uploadSession"/> /// information, and can control uploading of requests/> /// </summary> /// <param name="uploadSession">Session information of type <see cref="IUploadSession"/>></param> /// <param name="uploadStream">Readable, seekable stream to be uploaded. Length of session is determined via uploadStream.Length</param> /// <param name="maxSliceSize">Max size of each slice to be uploaded. Multiple of 320 KiB (320 * 1024) is required.</param> /// <param name="baseClient"><see cref="IBaseClient"/> to use for making upload requests. The client should not set Auth headers as upload urls do not need them. /// If less than 0, default value of 5 MiB is used. .</param> public LargeFileUploadTask(IUploadSession uploadSession, Stream uploadStream, int maxSliceSize = -1, IBaseClient baseClient = null) { if (!uploadStream.CanRead || !uploadStream.CanSeek) { throw new ArgumentException("Must provide stream that can read and seek"); } this.Session = uploadSession; this._client = baseClient ?? this.InitializeClient(uploadSession.UploadUrl); this._uploadStream = uploadStream; this._rangesRemaining = this.GetRangesRemaining(uploadSession); this._maxSliceSize = maxSliceSize < 0 ? DefaultMaxSliceSize : maxSliceSize; if (this._maxSliceSize % RequiredSliceSizeIncrement != 0) { throw new ArgumentException("Max slice size must be a multiple of 320 KiB", nameof(maxSliceSize)); } }
/// <summary> /// Create a new UploadSessionRequest /// </summary> /// <param name="session">The IUploadSession to use in the request.</param> /// <param name="client">The <see cref="IBaseClient"/> for handling requests.</param> public UploadSessionRequest(IUploadSession session, IBaseClient client) : base(session.UploadUrl, client, null) { this.responseHandler = new UploadResponseHandler(); }