internal B2File(B2Client b2Client, B2FileInfo file) { _b2Client = b2Client; _file = file; State = B2FileState.Present; }
internal B2File(B2Client b2Client, string bucketId, string newName) { _b2Client = b2Client; _file = new B2FileInfo { Action = B2FileAction.Upload, FileName = newName, BucketId = bucketId, ContentType = B2Constants.AutoContenType }; State = B2FileState.New; }
public async Task<B2File> UploadFileDataAsync(FileInfo file) { ThrowIfNot(B2FileState.New); B2UploadConfiguration config = _b2Client.FetchUploadConfig(BucketId); try { using (var fs = file.OpenRead()) { string sha1Hash; using (SHA1 sha1 = SHA1.Create()) sha1Hash = BitConverter.ToString(sha1.ComputeHash(fs)).Replace("-", ""); fs.Seek(0, SeekOrigin.Begin); B2FileInfo result = await _b2Client.Communicator.UploadFile(config.UploadUrl, config.AuthorizationToken, fs, sha1Hash, FileName, ContentType, FileInfo, _uploadNotifyDelegate); if (result.ContentSha1 != sha1Hash) throw new ArgumentException("Bad transfer - hash mismatch"); _file = result; } State = B2FileState.Present; } finally { _b2Client.ReturnUploadConfig(config); } return this; }
private void ThrowIfNot(B2FileState desiredState) { if (State != desiredState) throw new InvalidOperationException($"The B2 File, {FileName}, was {State} and not {desiredState} (id: {FileId})"); }
public override async Task<bool> DeleteAsync() { ThrowIfNot(B2FileState.Present); bool res = await _b2Client.Communicator.DeleteFile(FileName, FileId); State = B2FileState.Deleted; return res; }
public async Task<B2File> UploadDataAsync(Stream source) { ThrowIfNot(B2FileState.New); B2UploadConfiguration config = _b2Client.FetchUploadConfig(BucketId); try { B2FileInfo result = await _b2Client.Communicator.UploadFile(config.UploadUrl, config.AuthorizationToken, source, ContentSha1, FileName, ContentType, FileInfo, _uploadNotifyDelegate); if (result.ContentSha1 != ContentSha1) throw new ArgumentException("Bad transfer - hash mismatch"); _file = result; State = B2FileState.Present; } finally { _b2Client.ReturnUploadConfig(config); } return this; }