public override async Task <DriveFile> UploadFileAsync(Stream stream, string fileName, DriveFile destFolder, string storageFileId, CancellationToken token) { Stream scaled; var f = new FileInfo(((LocalFile)destFolder).file.FullName + Path.DirectorySeparatorChar + fileName); var localFile = LocalFileFactory.Instance.Create(this, f, false); ImageFormat imageType; if (!ImageMaxSize.IsEmpty && TryGetImageType(fileName, out imageType)) { using (var image = Image.FromStream(stream)) { scaled = await ScaleImageAsync(image, imageType, token); await localFile.SetAttributeAsync("OriginalImageHeight", image.Height.ToString(), token); await localFile.SetAttributeAsync("OriginalImageWidth", image.Width.ToString(), token); } } else { scaled = stream; } var observed = new ObserverStream(scaled); observed.PositionChanged += (sender, e) => { }; using (var outStream = f.Create()) { await scaled.CopyToAsync(outStream); } await localFile.SetStorageFileIdAsync(storageFileId, token); return(localFile); }
public override async Task <StorageFile> UploadFileAsync(Stream fileStream, string fn, StorageFile destFolder, CancellationToken token, Action <TransferProgress> progressCallback) { var path = ((GlacierFile)destFolder).FolderPath; if (!string.IsNullOrEmpty(path) && !path.EndsWith("/")) { path += "/"; } var filePathName = path + fn; var fileLen = fileStream.CanSeek ? fileStream.Length : 0; var checksum = await Task.Run(() => TreeHashGenerator.CalculateTreeHash(fileStream)); var observed = new ObserverStream(fileStream); var percent = 0; observed.PositionChanged += (sender, e) => { var currentPercent = fileLen == 0 ? 0 : (int)(100 * ((Stream)sender).Position / (float)fileLen); if (currentPercent == percent) { return; } percent = currentPercent; progressCallback(new TransferProgress(percent)); }; var req = new UploadArchiveRequest(Vault, filePathName.Unidecode(), checksum, observed); var result = await client.UploadArchiveAsync(req, token); return(new GlacierFile(this, result.ArchiveId, false, fn)); }
internal async Task <DriveFile> UploadFileAsync(System.IO.Stream stream, string fileName, GoogleFile destFolder, IList <Property> props, CancellationToken token) { var service = await GetServiceAsync(token); var file = new File { Properties = props, Title = fileName, Parents = new List <ParentReference> { new ParentReference { Id = destFolder == null ? RootId : destFolder.Id } } }; ImageFormat imageType; System.IO.Stream scaled; if (!ImageMaxSize.IsEmpty && TryGetImageType(fileName, out imageType)) { var image = await Task.Run(() => Image.FromStream(stream)); scaled = await ScaleImageAsync(image, imageType, token); } else { scaled = stream; } var observed = new ObserverStream(scaled); observed.PositionChanged += (sender, e) => { }; var request = service.Files.Insert(file, observed, ""); var result = await request.UploadAsync(token); if (result.Status == UploadStatus.Failed) { throw new Exception(result.Exception.Message); } return(new GoogleFile(this, request.ResponseBody)); }
internal async Task<DriveFile> UploadFileAsync(System.IO.Stream stream, string fileName, GoogleFile destFolder, IList<Property> props, CancellationToken token) { var service = await GetServiceAsync(token); var file = new File { Properties = props, Title = fileName, Parents = new List<ParentReference> { new ParentReference {Id = destFolder == null ? RootId : destFolder.Id} } }; ImageFormat imageType; System.IO.Stream scaled; if (!ImageMaxSize.IsEmpty && TryGetImageType(fileName, out imageType)) { var image = await Task.Run(() => Image.FromStream(stream)); scaled = await ScaleImageAsync(image, imageType, token); } else { scaled = stream; } var observed = new ObserverStream(scaled); observed.PositionChanged += (sender, e) => { }; var request = service.Files.Insert(file, observed, ""); var result = await request.UploadAsync(token); if (result.Status == UploadStatus.Failed) { throw new Exception(result.Exception.Message); } return new GoogleFile(this, request.ResponseBody); }
public override async Task<DriveFile> UploadFileAsync(Stream stream, string fileName, DriveFile destFolder, string storageFileId, CancellationToken token) { Stream scaled; var f = new FileInfo(((LocalFile)destFolder).file.FullName + Path.DirectorySeparatorChar + fileName); var localFile = LocalFileFactory.Instance.Create(this, f, false); ImageFormat imageType; if (!ImageMaxSize.IsEmpty && TryGetImageType(fileName, out imageType)) { using (var image = Image.FromStream(stream)) { scaled = await ScaleImageAsync(image, imageType, token); await localFile.SetAttributeAsync("OriginalImageHeight", image.Height.ToString(), token); await localFile.SetAttributeAsync("OriginalImageWidth", image.Width.ToString(), token); } } else { scaled = stream; } var observed = new ObserverStream(scaled); observed.PositionChanged += (sender, e) => { }; using (var outStream = f.Create()) { await scaled.CopyToAsync(outStream); } await localFile.SetStorageFileIdAsync(storageFileId, token); return localFile; }