Esempio n. 1
0
 private async Task UploadPartsInSessionAsync(Uri uploadPartsUri, int numberOfParts, long partSize, Stream stream,
                                              long fileSize)
 {
     for (int i = 0; i < numberOfParts; i++)
     {
         // Split file as per part size
         long   partOffset     = partSize * i;
         Stream partFileStream = UploadUsingSessionInternal.GetFilePart(stream, partSize, partOffset);
         string sha            = Helper.GetSha1Hash(partFileStream);
         partFileStream.Position = 0;
         await _client.FilesManager.UploadPartAsync(uploadPartsUri, sha, partOffset, fileSize, partFileStream);
     }
 }
Esempio n. 2
0
        public async Task GetNumberOfParts_Utility_Function_CorrectPartNumber()
        {
            // This file size is expected to divide evenly with the partSize
            long fileSize = 209717000;
            // This file size is expected to have a small remainder after dividing with partSize
            long divisibleFileSize = 209715200;
            long partSize          = 8388608;

            int numberOfPartsNoRemainder   = UploadUsingSessionInternal.GetNumberOfParts(divisibleFileSize, partSize);
            int numberOfPartsWithRemainder = UploadUsingSessionInternal.GetNumberOfParts(fileSize, partSize);

            Assert.AreEqual(numberOfPartsNoRemainder, 25);
            Assert.AreEqual(numberOfPartsWithRemainder, 26);
        }
Esempio n. 3
0
        public async Task UploadFileInSession_AbortRequest_FileNotCommmited()
        {
            long         fileSize           = 50000000;
            MemoryStream fileInMemoryStream = GetBigFileInMemoryStream(fileSize);
            string       remoteFileName     = "UploadedUsingSession-" + DateTime.Now.TimeOfDay;
            string       parentFolderId     = "0";

            BoxFileUploadSessionRequest boxFileUploadSessionRequest = new BoxFileUploadSessionRequest()
            {
                FolderId = parentFolderId,
                FileName = remoteFileName,
                FileSize = fileSize
            };
            // Create Upload Session
            BoxFileUploadSession boxFileUploadSession = await _client.FilesManager.CreateUploadSessionAsync(boxFileUploadSessionRequest);

            BoxSessionEndpoint boxSessionEndpoint = boxFileUploadSession.SessionEndpoints;
            Uri    abortUri      = new Uri(boxSessionEndpoint.Abort);
            Uri    uploadPartUri = new Uri(boxSessionEndpoint.UploadPart);
            string partSize      = boxFileUploadSession.PartSize;
            long   partSizeLong;

            long.TryParse(partSize, out partSizeLong);
            int numberOfParts = UploadUsingSessionInternal.GetNumberOfParts(fileSize, partSizeLong);

            // Upload parts in the session
            await UploadPartsInSessionAsync(uploadPartUri, numberOfParts, partSizeLong, fileInMemoryStream, fileSize);

            // Assert file is not committed/uploaded to box yet
            Assert.IsFalse(await DoesFileExistInFolder(parentFolderId, remoteFileName));

            // Abort
            await _client.FilesManager.DeleteUploadSessionAsync(abortUri);

            // Assert file is not committed/uploaded to box
            Assert.IsFalse(await DoesFileExistInFolder(parentFolderId, remoteFileName));
        }
Esempio n. 4
0
        public async Task UploadFileInSession_CommitSession_FilePresent()
        {
            long         fileSize           = 50000000;
            MemoryStream fileInMemoryStream = GetBigFileInMemoryStream(fileSize);
            string       remoteFileName     = "UploadedUsingSession-" + DateTime.Now.TimeOfDay;
            string       parentFolderId     = "0";

            BoxFileUploadSessionRequest boxFileUploadSessionRequest = new BoxFileUploadSessionRequest()
            {
                FolderId = parentFolderId,
                FileName = remoteFileName,
                FileSize = fileSize
            };
            // Create Upload Session
            BoxFileUploadSession boxFileUploadSession = await _client.FilesManager.CreateUploadSessionAsync(boxFileUploadSessionRequest);

            BoxSessionEndpoint boxSessionEndpoint = boxFileUploadSession.SessionEndpoints;
            Uri    listPartsUri  = new Uri(boxSessionEndpoint.ListParts);
            Uri    uploadPartUri = new Uri(boxSessionEndpoint.UploadPart);
            Uri    commitUri     = new Uri(boxSessionEndpoint.Commit);
            string partSize      = boxFileUploadSession.PartSize;
            long   partSizeLong;

            long.TryParse(partSize, out partSizeLong);
            int numberOfParts = UploadUsingSessionInternal.GetNumberOfParts(fileSize, partSizeLong);

            // Upload parts in the session
            await UploadPartsInSessionAsync(uploadPartUri, numberOfParts, partSizeLong, fileInMemoryStream, fileSize);

            // Assert file is not committed/uploaded to box yet
            Assert.IsFalse(await DoesFileExistInFolder(parentFolderId, remoteFileName));

            // Get upload parts (1 by 1) for Integration testing purposes
            List <BoxSessionPartInfo> allSessionParts = new List <BoxSessionPartInfo>();

            // var boxSessionParts = await _client.FilesManager.GetSessionUploadedPartsAsync(listPartsUri, 0, 2, true);
            var boxSessionParts = await _client.FilesManager.GetSessionUploadedPartsAsync(listPartsUri, null, null, true);

            foreach (var sessionPart in boxSessionParts.Entries)
            {
                allSessionParts.Add(sessionPart);
            }

            /* w/o autopaging
             * var boxSessionParts = await _client.FilesManager.GetSessionUploadedPartsAsync(listPartsUri, 0, 1);
             * allSessionParts.AddRange(boxSessionParts.Entries);
             *
             * while (allSessionParts.Count < boxSessionParts.TotalCount)
             * {
             *  boxSessionParts = await _client.FilesManager.GetSessionUploadedPartsAsync(listPartsUri, allSessionParts.Count, 1);
             *  allSessionParts.AddRange(boxSessionParts.Entries);
             * }
             */

            BoxSessionParts sessionPartsForCommit = new BoxSessionParts()
            {
                Parts = allSessionParts
            };

            // Commit
            await _client.FilesManager.CommitSessionAsync(commitUri, Helper.GetSha1Hash(fileInMemoryStream), sessionPartsForCommit);

            // Assert file is committed/uploaded to box
            Assert.IsTrue(await DoesFileExistInFolder(parentFolderId, remoteFileName));

            // Delete file
            string fileId = await GetFileId(parentFolderId, remoteFileName);

            if (!string.IsNullOrWhiteSpace(fileId))
            {
                await _client.FilesManager.DeleteAsync(fileId);
            }

            // Assert file has been deleted from Box
            Assert.IsFalse(await DoesFileExistInFolder(parentFolderId, remoteFileName));
        }