protected AsyncUploaderBase(ShareFileClient client, UploadSpecificationRequest uploadSpecificationRequest, IPlatformFile file, FileUploaderConfig config = null, int? expirationDays = null) : base(client, uploadSpecificationRequest, file, expirationDays) { Config = config ?? new FileUploaderConfig(); HashProvider = MD5HashProviderFactory.GetHashProvider().CreateHash(); }
protected AsyncUploaderBase(ShareFileClient client, UploadSpecificationRequest uploadSpecificationRequest, IPlatformFile file, FileUploaderConfig config = null, int?expirationDays = null) : base(client, uploadSpecificationRequest, file, expirationDays) { Config = config ?? new FileUploaderConfig(); HashProvider = MD5HashProviderFactory.GetHashProvider().CreateHash(); }
protected IQuery <UploadSpecification> CreateUploadSpecificationQuery(UploadSpecificationRequest uploadSpecificationRequest) { if (uploadSpecificationRequest.ProviderCapabilities.SupportsUploadWithRequestParams()) { return(CreateUploadRequestParamsQuery(uploadSpecificationRequest)); } var query = Client.Items.Upload(uploadSpecificationRequest.Parent, uploadSpecificationRequest.Method.GetValueOrDefault(UploadMethod.Threaded), uploadSpecificationRequest.Raw, uploadSpecificationRequest.FileName, uploadSpecificationRequest.FileSize, uploadSpecificationRequest.BatchId, uploadSpecificationRequest.BatchLast, uploadSpecificationRequest.CanResume, uploadSpecificationRequest.StartOver, uploadSpecificationRequest.Unzip, uploadSpecificationRequest.Tool, uploadSpecificationRequest.Overwrite, uploadSpecificationRequest.Title, uploadSpecificationRequest.Details, uploadSpecificationRequest.IsSend, uploadSpecificationRequest.SendGuid, null, uploadSpecificationRequest.ThreadCount, uploadSpecificationRequest.ResponseFormat, uploadSpecificationRequest.Notify, uploadSpecificationRequest.ClientCreatedDateUtc, uploadSpecificationRequest.ClientModifiedDateUtc, ExpirationDays, uploadSpecificationRequest.BaseFileId); return(query); }
protected IQuery <UploadSpecification> CreateUploadRequestParamsQuery( UploadSpecificationRequest uploadSpecificationRequest) { var query = Client.Items.Upload2(uploadSpecificationRequest.Parent, uploadSpecificationRequest.ToRequestParams(), ExpirationDays); return(query); }
void IAction.CopyFileItem(ProgressInfo progressInfo) { var fileInfo = (FileInfo)child; Models.Item fileItem = null; fileName = child.Name; try { fileItem = client.Items.ByPath(uploadTarget.url, "/" + child.Name).Execute(); } catch (ODataException e) { if (e.Code != System.Net.HttpStatusCode.NotFound) { throw e; } } bool duplicate = fileItem != null && fileItem is Models.File; bool hashcodeMatches = duplicate ? (fileItem as Models.File).Hash.Equals(Utility.GetMD5HashFromFile(child.FullName)) : false; if (duplicate && actionType == ActionType.None) { throw new IOException("File already exist"); } else if (!duplicate || actionType == ActionType.Force || (actionType == ActionType.Sync && !hashcodeMatches)) { var uploadSpec = new UploadSpecificationRequest { CanResume = false, Details = details, FileName = fileInfo.Name, FileSize = fileInfo.Length, Method = UploadMethod.Threaded, Parent = uploadTarget.url, ThreadCount = 4, Raw = true }; using (var platformFileInfo = new PlatformFileInfo(fileInfo)) { var uploader = client.GetAsyncFileUploader(uploadSpec, platformFileInfo); progressInfo.ProgressTotal(progressInfo.FileIndex, fileInfo.Length); uploader.OnTransferProgress = (sender, args) => { if (args.Progress.TotalBytes > 0) { progressInfo.ProgressTransferred(progressInfo.FileIndex, args.Progress.BytesTransferred); } }; Task.Run(() => uploader.UploadAsync()).Wait(); fileSupportDelegate(fileInfo.Name); } } }
public ScalingFileUploader(ShareFileClient client, UploadSpecificationRequest uploadSpecificationRequest, IPlatformFile file, FileUploaderConfig config = null, int? expirationDays = null) : base(client, uploadSpecificationRequest, file, config, expirationDays) { UploadSpecificationRequest.Raw = true; var partConfig = config != null ? config.PartConfig : new FilePartConfig(); partUploader = new ScalingPartUploader(partConfig, Config.NumberOfThreads, requestMessage => Task.Factory.StartNew(() => ExecuteChunkUploadMessage(requestMessage)), OnProgress); }
protected UploaderBase(ShareFileClient client, UploadSpecificationRequest uploadSpecificationRequest, IPlatformFile file, int? expirationDays) { Client = client; UploadSpecificationRequest = uploadSpecificationRequest; File = file; ExpirationDays = expirationDays; Progress = new TransferProgress(uploadSpecificationRequest.FileSize, null, Guid.NewGuid().ToString()); }
public AsyncScalingFileUploader(ShareFileClient client, UploadSpecificationRequest uploadSpecificationRequest, IPlatformFile file, FileUploaderConfig config = null, int? expirationDays = null) : base(client, uploadSpecificationRequest, file, config, expirationDays) { UploadSpecificationRequest.Raw = true; var chunkConfig = config != null ? config.PartConfig : new FilePartConfig(); partUploader = new ScalingPartUploader(chunkConfig, Config.NumberOfThreads, ExecuteChunkUploadMessage, OnProgress); }
protected UploaderBase(ShareFileClient client, UploadSpecificationRequest uploadSpecificationRequest, IPlatformFile file, int?expirationDays) { Client = client; UploadSpecificationRequest = uploadSpecificationRequest; File = file; ExpirationDays = expirationDays; Progress = new TransferProgress(uploadSpecificationRequest.FileSize, null, Guid.NewGuid().ToString()); }
public StandardFileUploader( ShareFileClient client, UploadSpecificationRequest uploadSpecificationRequest, Stream stream, FileUploaderConfig config = null, int?expirationDays = null) : base(client, uploadSpecificationRequest, stream, config, expirationDays) { }
void IAction.CopyFileItem() { var fileInfo = (FileInfo)child; Models.Item fileItem = null; try { fileItem = client.Items.ByPath(uploadTarget.url, "/" + child.Name).Execute(); } catch (ODataException e) { if (e.Code != System.Net.HttpStatusCode.NotFound) { throw e; } } bool duplicate = fileItem != null && fileItem is Models.File; bool hashcodeMatches = duplicate ? (fileItem as Models.File).Hash.Equals(Utility.GetMD5HashFromFile(child.FullName)) : false; if (duplicate && actionType == ActionType.None) { throw new IOException("File already exist"); } else if (!duplicate || actionType == ActionType.Force || (actionType == ActionType.Sync && !hashcodeMatches)) { var uploadSpec = new UploadSpecificationRequest { CanResume = false, Details = details, FileName = fileInfo.Name, FileSize = fileInfo.Length, Method = UploadMethod.Threaded, Parent = uploadTarget.url, ThreadCount = 4, Raw = true }; var uploader = client.GetAsyncFileUploader(uploadSpec, new PlatformFileInfo(fileInfo)); var progressBar = new ProgressBar(copySfItem); progressBar.SetProgress(fileInfo.Name, 0); uploader.OnTransferProgress = (sender, args) => { int pct = 100; if (args.Progress.TotalBytes > 0) { pct = (int)(((float)args.Progress.BytesTransferred / args.Progress.TotalBytes) * 100); } progressBar.SetProgress(fileInfo.Name, pct); }; Task task = Task.Run(() => uploader.UploadAsync()); task.ContinueWith(t => progressBar.Finish()); task.Wait(); } }
protected AsyncUploaderBase( ShareFileClient client, UploadSpecificationRequest uploadSpecificationRequest, Stream stream, FileUploaderConfig config = null, int?expirationDays = null) : base(client, uploadSpecificationRequest, stream, config, expirationDays) { HashProvider = MD5HashProviderFactory.GetHashProvider().CreateHash(); }
public AsyncScalingFileUploader(ShareFileClient client, UploadSpecificationRequest uploadSpecificationRequest, IPlatformFile file, FileUploaderConfig config = null, int?expirationDays = null) : base(client, uploadSpecificationRequest, file, config, expirationDays) { UploadSpecificationRequest.Raw = true; var chunkConfig = config != null ? config.PartConfig : new FilePartConfig(); partUploader = new ScalingPartUploader(chunkConfig, Config.NumberOfThreads, ExecuteChunkUploadMessage, OnProgress); }
public ScalingFileUploader(ShareFileClient client, UploadSpecificationRequest uploadSpecificationRequest, IPlatformFile file, FileUploaderConfig config = null, int?expirationDays = null) : base(client, uploadSpecificationRequest, file, config, expirationDays) { UploadSpecificationRequest.Raw = true; var partConfig = config != null ? config.PartConfig : new FilePartConfig(); partUploader = new ScalingPartUploader(partConfig, Config.NumberOfThreads, requestMessage => Task.Factory.StartNew(() => ExecuteChunkUploadMessage(requestMessage)), OnProgress); }
public StandardFileUploader( ShareFileClient client, UploadSpecification uploadSpecification, UploadSpecificationRequest uploadSpecificationRequest, Stream stream, FileUploaderConfig config = null) : this(client, uploadSpecificationRequest, stream, config, expirationDays : null) { UploadSpecification = uploadSpecification; }
public AsyncScalingFileUploader( ShareFileClient client, ActiveUploadState activeUploadState, UploadSpecificationRequest uploadSpecificationRequest, Stream stream, FileUploaderConfig config = null) : this(client, uploadSpecificationRequest, stream, config) { this.activeUploadState = activeUploadState; UploadSpecification = activeUploadState.UploadSpecification; }
/// <summary> /// Use some naive metrics for deciding which <see cref="UploadMethod"/> should be used. /// </summary> /// <param name="uploadSpecificationRequest"></param> private void PreprocessUploadSpecRequest(UploadSpecificationRequest uploadSpecificationRequest) { if (string.IsNullOrEmpty(uploadSpecificationRequest.Tool)) { uploadSpecificationRequest.Tool = Configuration.ToolName; } if (uploadSpecificationRequest.Method.HasValue) { return; } uploadSpecificationRequest.Method = GetUploadMethod(uploadSpecificationRequest.FileSize); }
protected UploaderBase(ShareFileClient client, UploadSpecificationRequest uploadSpecificationRequest, Stream stream, FileUploaderConfig config, int?expirationDays) { Client = client; UploadSpecificationRequest = uploadSpecificationRequest; FileStream = stream; Config = config ?? new FileUploaderConfig(); if (uploadSpecificationRequest.FileSize != stream.Length) { throw new UploadException("Specified file size does not equal file stream length", UploadStatusCode.Unknown); } ExpirationDays = expirationDays; progressReporter = new TransferProgressReporter(uploadSpecificationRequest.FileSize, Guid.NewGuid().ToString(), Config.ProgressReportInterval); }
public AsyncMemoryMappedFileUploader( ShareFileClient client, UploadSpecificationRequest uploadSpecificationRequest, FileStream stream, FileUploaderConfig config = null, ActiveUploadState activeUploadState = null, int?expirationDays = default(int?)) : base(client, uploadSpecificationRequest, stream, config, expirationDays) { hasher = new FileChunkHasher(HashProvider); if (activeUploadState != null) { initialPosition = activeUploadState.BytesUploaded; UploadSpecification = activeUploadState.UploadSpecification; // what happens if the new UploadSpecRequest disagrees with this? } }
public AsyncScalingFileUploader( ShareFileClient client, UploadSpecificationRequest uploadSpecificationRequest, Stream stream, FileUploaderConfig config = null, int?expirationDays = null) : base(client, uploadSpecificationRequest, stream, config, expirationDays) { var chunkConfig = config != null ? config.PartConfig : new FilePartConfig(); partUploader = new ScalingPartUploader( chunkConfig, Config.NumberOfThreads, ExecuteChunkUploadMessage, progressReporter.ReportProgress, client.Logging); }
public static async Task <string> Upload(ShareFileClient sfClient, Folder destinationFolder) { var file = System.IO.File.Open("SampleFileUpload.txt", FileMode.OpenOrCreate); var uploadRequest = new UploadSpecificationRequest { FileName = "SampleFileUpload.txt", FileSize = file.Length, Details = "Sample details", Parent = destinationFolder.url }; var uploader = sfClient.GetAsyncFileUploader(uploadRequest, file); var uploadResponse = await uploader.UploadAsync(); return(uploadResponse.First().Id); }
public ScalingFileUploader( ShareFileClient client, UploadSpecificationRequest uploadSpecificationRequest, Stream stream, FileUploaderConfig config = null, int?expirationDays = null) : base(client, uploadSpecificationRequest, stream, config, expirationDays) { var partConfig = config != null ? config.PartConfig : new FilePartConfig(); partUploader = new ScalingPartUploader( partConfig, Config.NumberOfThreads, (requestMessage, cancelToken) => Task.Factory.StartNew(() => ExecuteChunkUploadMessage(requestMessage, cancelToken)), progressReporter.ReportProgress, client.Logging); }
/// <summary> /// /// </summary> /// <param name="uploadSpecificationRequest"></param> /// <param name="file"></param> /// <param name="config"></param> /// <param name="expirationDays">Will only be obeyed by ShareFile apps</param> /// <returns></returns> public SyncUploaderBase GetFileUploader( UploadSpecificationRequest uploadSpecificationRequest, Stream stream, FileUploaderConfig config = null, int?expirationDays = null) { this.PreprocessUploadSpecRequest(uploadSpecificationRequest); switch (uploadSpecificationRequest.Method) { case UploadMethod.Standard: return(new StandardFileUploader(this, uploadSpecificationRequest, stream, config, expirationDays)); case UploadMethod.Threaded: return(new ScalingFileUploader(this, uploadSpecificationRequest, stream, config, expirationDays)); } throw new NotSupportedException(uploadSpecificationRequest.Method + " is not supported."); }
/// <summary> /// /// </summary> /// <param name="uploadSpecificationRequest"></param> /// <param name="file"></param> /// <param name="config"></param> /// <param name="expirationDays">Will only be obeyed by ShareFile apps</param> /// <returns></returns> public AsyncUploaderBase GetAsyncFileUploader(UploadSpecificationRequest uploadSpecificationRequest, IPlatformFile file, FileUploaderConfig config = null, int?expirationDays = null) { this.PreprocessUploadSpecRequest(uploadSpecificationRequest); switch (uploadSpecificationRequest.Method) { case UploadMethod.Standard: return(new AsyncStandardFileUploader(this, uploadSpecificationRequest, file, config, expirationDays)); break; case UploadMethod.Threaded: return(new AsyncScalingFileUploader(this, uploadSpecificationRequest, file, config, expirationDays)); break; } throw new NotSupportedException(uploadSpecificationRequest.Method + " is not supported."); }
/// <summary> /// Upload File using the Sharefile C# SDK /// </summary> /// <param name="hostname"></param> /// <param name="parentId"></param> /// <param name="localPath"></param> public void UploadFile(string hostname, string parentId, string localPath) { Console.WriteLine("Starting Upload with SDK"); var baseUri = string.Format("https://{0}/sf/v3", hostname); var sfClient = new ShareFileClient(baseUri); var shareUri = sfClient.Shares.GetEntityUriFromId(parentId); using (var file = File.Open(localPath, FileMode.OpenOrCreate)) { var uploadRequest = new UploadSpecificationRequest { FileName = Path.GetFileName(localPath), FileSize = file.Length, Parent = shareUri }; var uploader = sfClient.GetFileUploader(uploadRequest, file); uploader.Upload(); } Console.WriteLine("SDK Upload completed"); }
/// <summary> /// Use some naive metrics for deciding which <see cref="UploadMethod"/> should be used. /// </summary> /// <param name="uploadSpecificationRequest"></param> private void PreprocessUploadSpecRequest(UploadSpecificationRequest uploadSpecificationRequest) { if (string.IsNullOrEmpty(uploadSpecificationRequest.Tool)) { uploadSpecificationRequest.Tool = Configuration.ToolName; } if (uploadSpecificationRequest.Method.HasValue) { return; } if (uploadSpecificationRequest.ProviderCapabilities == null) { uploadSpecificationRequest.ProviderCapabilities = GetCachedCapabilities(uploadSpecificationRequest.Parent); } if (uploadSpecificationRequest.ProviderCapabilities != null && uploadSpecificationRequest.Method == null) { if (uploadSpecificationRequest.ProviderCapabilities.Any(x => x.Name == CapabilityName.StandardUploadRaw)) { uploadSpecificationRequest.Method = this.GetUploadMethod(uploadSpecificationRequest.FileSize); } else if (uploadSpecificationRequest.ProviderCapabilities.Any(x => x.Name == CapabilityName.ThreadedUploadRaw)) { uploadSpecificationRequest.Method = UploadMethod.Threaded; } else { // Bug - 4.4.2016 - SFSZP-451 - RZ doesn't return upload capabilities so default to this even it it might not work uploadSpecificationRequest.Method = this.GetUploadMethod(uploadSpecificationRequest.FileSize); } } else { uploadSpecificationRequest.Method = UploadMethod.Threaded; } }
public async Task <bool> UploadFile(Stream stream, string parentFolderId, string fileName) { if (_client == null) { _client = await GetShareFileClient(); } var parentFolder = (Folder)await _client.Items.Get(BuildUriFromId(parentFolderId), false).ExecuteAsync(); var uploadRequest = new UploadSpecificationRequest { FileName = fileName, FileSize = stream.Length, Details = string.Empty, Parent = parentFolder.url, Overwrite = true }; var uploader = _client.GetAsyncFileUploader(uploadRequest, new PlatformFileStream(stream, stream.Length, fileName)); var uploadResponse = await uploader.UploadAsync(); return(uploadResponse.FirstOrDefault() != null); }
public SyncUploaderBase GetFileUploader( ActiveUploadState activeUploadState, UploadSpecificationRequest uploadSpecificationRequest, Stream stream, FileUploaderConfig config = null) { UploadMethod?method = activeUploadState.UploadSpecification.Method; if (!method.HasValue) { throw new ArgumentNullException("UploadSpecification.Method"); } switch (method.Value) { case UploadMethod.Standard: return(new StandardFileUploader(this, activeUploadState.UploadSpecification, uploadSpecificationRequest, stream, config)); case UploadMethod.Threaded: return(new ScalingFileUploader(this, activeUploadState, uploadSpecificationRequest, stream, config)); default: throw new NotSupportedException($"{method} is not supported"); } }
public AsyncUploaderBase GetAsyncFileUploader( ActiveUploadState activeUploadState, UploadSpecificationRequest uploadSpecificationRequest, Stream stream, FileUploaderConfig config = null) { this.PreprocessUploadSpecRequest(uploadSpecificationRequest); switch (uploadSpecificationRequest.Method) { case UploadMethod.Standard: return(new AsyncStandardFileUploader(this, uploadSpecificationRequest, stream, config)); case UploadMethod.Threaded: return(new AsyncScalingFileUploader( this, activeUploadState, uploadSpecificationRequest, stream, config)); } throw new NotSupportedException(uploadSpecificationRequest.Method + " is not supported."); }
public async void Upload(UploadMethod uploadMethod, int megabytes, bool useAsync) { var shareFileClient = GetShareFileClient(); var rootFolder = shareFileClient.Items.Get().Execute(); var testFolder = new Folder { Name = RandomString(30) + ".txt" }; testFolder = shareFileClient.Items.CreateFolder(rootFolder.url, testFolder).Execute(); var file = GetFileToUpload(1024 * 1024 * megabytes); var uploadSpec = new UploadSpecificationRequest(file.Name, file.Length, testFolder.url, uploadMethod); UploaderBase uploader; if (useAsync) { uploader = shareFileClient.GetAsyncFileUploader(uploadSpec, file); } else { uploader = shareFileClient.GetFileUploader(uploadSpec, file); } var progressInvocations = 0; var bytesTransferred = 0L; uploader.OnTransferProgress += (sender, args) => { bytesTransferred = args.Progress.BytesTransferred; progressInvocations++; }; UploadResponse uploadResponse; if (useAsync) { uploadResponse = await((AsyncUploaderBase)uploader).UploadAsync(); } else { uploadResponse = ((SyncUploaderBase)uploader).Upload(); } shareFileClient.Items.Delete(testFolder.url); uploadResponse.FirstOrDefault().Should().NotBeNull(); var expectedInvocations = Math.Ceiling((double)file.Length / UploaderBase.DefaultBufferLength) + 1; bytesTransferred.Should().Be(1024 * 1024 * megabytes); if (uploadMethod == UploadMethod.Standard) { progressInvocations.Should().Be((int)expectedInvocations, "Standard should be predictable for number of progress callbacks"); } else if (uploadMethod == UploadMethod.Threaded) { progressInvocations.Should() .BeLessOrEqualTo( (int)expectedInvocations, "Threaded scales, therefore byte ranges vary and are less predictable. We should see no more expectedInvoations"); } }
protected IQuery<UploadSpecification> CreateUploadSpecificationQuery(UploadSpecificationRequest uploadSpecificationRequest) { if (uploadSpecificationRequest.ProviderCapabilities.SupportsUploadWithRequestParams()) { return CreateUploadRequestParamsQuery(uploadSpecificationRequest); } var query = Client.Items.Upload(uploadSpecificationRequest.Parent, uploadSpecificationRequest.Method.GetValueOrDefault(UploadMethod.Threaded), uploadSpecificationRequest.Raw, uploadSpecificationRequest.FileName, uploadSpecificationRequest.FileSize, uploadSpecificationRequest.BatchId, uploadSpecificationRequest.BatchLast, uploadSpecificationRequest.CanResume, uploadSpecificationRequest.StartOver, uploadSpecificationRequest.Unzip, uploadSpecificationRequest.Tool, uploadSpecificationRequest.Overwrite, uploadSpecificationRequest.Title, uploadSpecificationRequest.Details, uploadSpecificationRequest.IsSend, uploadSpecificationRequest.SendGuid, null, uploadSpecificationRequest.ThreadCount, uploadSpecificationRequest.ResponseFormat, uploadSpecificationRequest.Notify, uploadSpecificationRequest.ClientCreatedDateUtc, uploadSpecificationRequest.ClientModifiedDateUtc, ExpirationDays); return query; }
protected IQuery<UploadSpecification> CreateUploadRequestParamsQuery( UploadSpecificationRequest uploadSpecificationRequest) { var query = Client.Items.Upload2(uploadSpecificationRequest.Parent, uploadSpecificationRequest.ToRequestParams(), ExpirationDays); return query; }
public AsyncStandardFileUploader(ShareFileClient client, UploadSpecificationRequest uploadSpecificationRequest, IPlatformFile file, FileUploaderConfig config = null, int? expirationDays = null) : base(client, uploadSpecificationRequest, file, config, expirationDays) { UploadSpecificationRequest.Raw = true; }
public AsyncStandardFileUploader(ShareFileClient client, UploadSpecificationRequest uploadSpecificationRequest, IPlatformFile file, FileUploaderConfig config = null, int?expirationDays = null) : base(client, uploadSpecificationRequest, file, config, expirationDays) { UploadSpecificationRequest.Raw = false; }