// ReSharper disable once UnusedMember.Local
        private async Task <UploadSession> DeleteUploadSessionAsync(IFileServiceClient fileServiceClient, int resourceId)
        {
            if (fileServiceClient == null)
            {
                throw new ArgumentNullException(nameof(fileServiceClient));
            }

            if (resourceId <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(resourceId));
            }

            var result = await fileServiceClient.DeleteUploadSessionAsync(resourceId);

            switch (result.StatusCode)
            {
            case HttpStatusCode.OK:
                return(result.Session);

            default:
                OnUploadError(new UploadErrorEventArgs(resourceId, result.FullUri, result.StatusCode.ToString(),
                                                       result.Error));
                throw new FileUploaderException(result.FullUri, result.StatusCode.ToString(), result.Error);
            }
        }
        private async Task <UploadSession> CreateNewUploadSessionAsync(IFileServiceClient fileServiceClient,
                                                                       int resourceId)
        {
            if (fileServiceClient == null)
            {
                throw new ArgumentNullException(nameof(fileServiceClient));
            }

            if (resourceId <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(resourceId));
            }

            var result = await fileServiceClient.CreateNewUploadSessionAsync(resourceId);

            switch (result.StatusCode)
            {
            case HttpStatusCode.OK:
                OnSessionCreated(new SessionCreatedEventArgs(
                                     resourceId, result.Session.SessionId, result.Session.FileUploadChunkSizeInBytes,
                                     result.Session.FileUploadMaxFileSizeInMegabytes,
                                     result.Session.SessionStartedBy, result.Session.SessionStartedDateTimeUtc,
                                     result.Session.FileUploadSessionExpirationInMinutes));

                return(result.Session);

            default:
                OnUploadError(new UploadErrorEventArgs(resourceId, result.FullUri, result.StatusCode.ToString(),
                                                       result.Error));
                throw new FileUploaderException(result.FullUri, result.StatusCode.ToString(), result.Error);
            }
        }
示例#3
0
 public static async Task <DataResult <FileUploadDataResult> > UploadAsync(this IFileServiceClient client, string ownerToken, byte[] fileBytes, string fileName, int periodMinute = 0)
 {
     using (var ms = new MemoryStream(fileBytes, false))
     {
         return(await client.UploadAsync(ownerToken, ms, fileName, periodMinute));
     }
 }
示例#4
0
        public static async Task <DataResult <FileUploadDataResult> > UploadAsync(this IFileServiceClient client, string ownerToken, string filePath, int periodMinute = 0)
        {
            var fi = new FileInfo(filePath);

            if (!fi.Exists)
            {
                throw new FileNotFoundException(filePath);
            }

            using (var fs = File.OpenRead(filePath))
            {
                return(await client.UploadAsync(ownerToken, fs, fi.Name, periodMinute));
            }
        }
        private async Task <SetUploadedResult> SetUploadedAsync(IFileServiceClient fileServiceClient, int resourceId)
        {
            if (fileServiceClient == null)
            {
                throw new ArgumentNullException(nameof(fileServiceClient));
            }

            if (resourceId <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(resourceId));
            }

            var result = await fileServiceClient.SetUploadedAsync(resourceId);

            return(result);
        }
        private async Task <CommitResult> CheckCommitAsync(
            IFileServiceClient fileServiceClient,
            int resourceId,
            Guid sessionId,
            string fileName)
        {
            if (fileServiceClient == null)
            {
                throw new ArgumentNullException(nameof(fileServiceClient));
            }

            if (resourceId <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(resourceId));
            }
            if (sessionId == default(Guid))
            {
                throw new ArgumentOutOfRangeException(nameof(sessionId));
            }

            var result = await fileServiceClient.CheckCommitAsync(resourceId, sessionId);

            switch (result.StatusCode)
            {
            case HttpStatusCode.OK:
                OnFileUploadCompleted(new FileUploadCompletedEventArgs(
                                          resourceId,
                                          sessionId,
                                          fileName,
                                          result.Session.FileHash,
                                          result.Session.SessionStartedDateTimeUtc,
                                          result.Session.SessionFinishedDateTimeUtc,
                                          result.Session.SessionStartedBy));

                return(result);

            case HttpStatusCode.Accepted:
            {
                return(result);
            }

            default:
                OnUploadError(new UploadErrorEventArgs(resourceId, result.FullUri, result.StatusCode.ToString(), result.Error));
                throw new FileUploaderException(result.FullUri, result.StatusCode.ToString(), result.Error);
            }
        }
        private async Task UploadFilePartStreamAsync(
            IFileServiceClient fileServiceClient,
            Stream stream,
            FilePart filePart,
            int resourceId,
            Guid sessionId,
            string fileName,
            long fullFileSize,
            int filePartsCount)
        {
            if (fileServiceClient == null)
            {
                throw new ArgumentNullException(nameof(fileServiceClient));
            }

            if (filePart == null)
            {
                throw new ArgumentNullException(nameof(filePart));
            }
            if (fileName == null)
            {
                throw new ArgumentNullException(nameof(fileName));
            }
            if (resourceId <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(resourceId));
            }
            if (fullFileSize <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(fullFileSize));
            }
            if (filePartsCount <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(filePartsCount));
            }
            if (sessionId == default(Guid))
            {
                throw new ArgumentOutOfRangeException(nameof(sessionId));
            }


            var result = await fileServiceClient.UploadStreamAsync(resourceId, sessionId, stream, filePart, fileName, fullFileSize, filePartsCount);

            switch (result.StatusCode)
            {
            case HttpStatusCode.OK:
                TimeSpan timeElapsed             = this.watch.Elapsed;
                var      ticksPerPart            = timeElapsed.Ticks / result.PartsUploaded;
                var      estimatedTotalTicksLeft = ticksPerPart * (filePartsCount - result.PartsUploaded);

                var estimatedTimeRemaining = new TimeSpan(estimatedTotalTicksLeft);

                OnPartUploaded(
                    new PartUploadedEventArgs(resourceId, sessionId, fileName, filePart, result.StatusCode.ToString(), filePartsCount, result.PartsUploaded, estimatedTimeRemaining));

                break;

            default:
                OnUploadError(new UploadErrorEventArgs(resourceId, result.FullUri, result.StatusCode.ToString(), result.Error));
                throw new FileUploaderException(result.FullUri, result.StatusCode.ToString(), result.Error);
            }
        }
        private async Task <UploaderCheckFileResult> CheckIfFileExistsOnServerAsync(IFileServiceClient fileServiceClient, int resourceId,
                                                                                    string filePath, long fullFileSize)
        {
            if (fileServiceClient == null)
            {
                throw new ArgumentNullException(nameof(fileServiceClient));
            }

            if (resourceId <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(resourceId));
            }

            var result = await fileServiceClient.CheckFileAsync(resourceId);

            switch (result.StatusCode)
            {
            case HttpStatusCode.NoContent:
            {
                // if server has the file and the filename is the same then do a hash check to see if file needs uploading
                var fileNameOnServer = Path.GetFileName(result.FileNameOnServer);
                if (fileNameOnServer != null &&
                    fileNameOnServer.Equals(Path.GetFileName(filePath)) &&
                    result.HashForFileOnServer != null)
                {
                    OnCalculatingHash(new CalculatingHashEventArgs(resourceId, filePath, fullFileSize));
                    var hashForFile = new MD5FileHasher().CalculateHashForFile(filePath);

                    if (hashForFile == result.HashForFileOnServer &&
                        Path.GetFileName(filePath) == fileNameOnServer)
                    {
                        OnFileChecked(new FileCheckedEventArgs(resourceId, true, hashForFile,
                                                               result.HashForFileOnServer, result.LastModified,
                                                               fileNameOnServer, true));

                        return(new UploaderCheckFileResult
                            {
                                FileNeedsUploading = false,
                                HashForFile = hashForFile,
                                HashForFileOnServer = result.HashForFileOnServer,
                                FileNameOnServer = fileNameOnServer
                            });
                    }
                    else
                    {
                        OnFileChecked(new FileCheckedEventArgs(resourceId, true, hashForFile,
                                                               result.HashForFileOnServer, result.LastModified,
                                                               fileNameOnServer, false));

                        return(new UploaderCheckFileResult
                            {
                                FileNeedsUploading = true,
                                HashForFile = hashForFile,
                                HashForFileOnServer = result.HashForFileOnServer,
                                FileNameOnServer = fileNameOnServer
                            });
                    }
                }

                break;
            }

            case HttpStatusCode.NotFound:
                // this is acceptable response if the file does not exist on the server
                OnFileChecked(new FileCheckedEventArgs(resourceId, false, null, null, null, null, false));

                return(new UploaderCheckFileResult
                {
                    FileNeedsUploading = true,
                });

            default:
                OnUploadError(new UploadErrorEventArgs(resourceId, result.FullUri, result.StatusCode.ToString(),
                                                       result.Error));
                throw new FileUploaderException(result.FullUri, result.StatusCode.ToString(), result.Error);
            }

            return(new UploaderCheckFileResult
            {
                FileNeedsUploading = true
            });
        }