public async Task InitializeDownload(Form OwnerForm, string path, List <AbstractFile> DownloadList) { int count = DownloadList.Count, enumerator = 0; CopyProgress window = new CopyProgress("0", count.ToString()); window.Owner = OwnerForm; window.Show(); string ResultPath = String.Empty; foreach (ConcreteFile file in DownloadList) { /*if (DownloadList.Count < 2) * { * ResultPath = path; * } * else * { * ResultPath = path + @"\" + file.FileName; * }*/ ResultPath = (DownloadList.Count < 2) ? path : path + @"\" + file.FileName; using (FileStream fstream = new FileStream(ResultPath, FileMode.CreateNew)) { IDownloadResponse <FileMetadata> GetFile = await client.Files.DownloadAsync(file.FilePath); byte[] FileAsBytes = await GetFile.GetContentAsByteArrayAsync(); await fstream.WriteAsync(FileAsBytes, 0, FileAsBytes.Length); enumerator++; window.RefreshData(enumerator, count); } } }
public async Task InitializeDelete(Form OwnerForm, List <AbstractFile> DeleteList) { int count = DeleteList.Count, enumerator = 0; CopyProgress window = new CopyProgress("0", count.ToString()); window.Owner = OwnerForm; window.Show(); foreach (AbstractFile file in DeleteList) { if (file.Type() == FileDir.File) { ConcreteFile FileToDelete = (ConcreteFile)file; await client.Files.DeleteAsync(new DeleteArg(FileToDelete.FilePath)); enumerator++; window.RefreshData(enumerator, count); } if (file.Type() == FileDir.Folder) { ConcreteFolder FolderToDelete = (ConcreteFolder)file; await client.Files.DeleteAsync(new DeleteArg(FolderToDelete.FolderPath)); enumerator++; window.RefreshData(enumerator, count); } } window.Close(); }
protected virtual void OnCopyProgress(string currentfile, long filesize, double fileprogress, int currentBlockSize) { if (_pReport != null) { double persentage = Math.Min(100, (fileprogress * 100.0) / filesize); _pReport.CurrentFileSize = filesize; _pReport.CurrentFilePorgress = persentage; _pReport.CurrentFile = currentfile; _totalProcessed += currentBlockSize; _pReport.TotalSize = _totalSize; _pReport.OverAllProgress = Math.Min(100, (_totalProcessed * 100.0) / _totalSize); CopyProgress?.Invoke(this, _pReport); } }
public IEnumerator CopyAssets(string[] keys) { foreach (string item in keys) { yield return(StartCoroutine(WebUtil.Download($"{"file:///"}{PathUtil.GetPath(PathType.StreamingAssetsPath, "Res", PathUtil.GetPlatformForAssetBundle())}/{item}", (byte[] bytes) => { FileUtil.SaveAsset(PathUtil.GetPath(PathType.PersistentDataPath, "Res", PathUtil.GetPlatformForAssetBundle()), item, bytes); currentCopyCount += 1; Message?.Invoke($"初始化资源 {currentCopyCount} / {copyCount}"); CopyProgress?.Invoke(currentCopyCount, copyCount); }))); } //更新资源 UpdateAssets(string.Empty); }
public void TestCopyProgress() { const int time = 2; const int bytesPerSecond = 100; const int bytesTransferred = 500; const int expectedBytes = 2001; ICopyProgress p = new CopyProgress(TimeSpan.FromSeconds(time), bytesPerSecond, bytesTransferred, expectedBytes); Assert.AreEqual((double)bytesTransferred / expectedBytes, p.PercentComplete); Assert.AreEqual(TimeSpan.FromSeconds(time), p.TransferTime); Assert.AreEqual(bytesPerSecond, p.BytesPerSecond); Assert.AreEqual(bytesTransferred, p.BytesTransferred); Assert.AreEqual(expectedBytes, p.ExpectedBytes); }
public void PerformUpdate() { CopyProgress nevent = new CopyProgress(CopyProgress); update.OnProgress += nevent; working = true; try { update.Update(tfiles); } finally { working = false; update.OnProgress -= nevent; } }
public async Task InitializeUpload(Form OwnerForm, List <AbstractFile> UploadList, string path) { int count = UploadList.Count, enumerator = 0; CopyProgress window = new CopyProgress("0", count.ToString()); window.Owner = OwnerForm; window.Show(); foreach (ConcreteFile file in UploadList) { using (FileStream stream = new FileStream(file.FilePath, FileMode.Open)) { await client.Files.UploadAsync(path + "/" + file.FileName, WriteMode.Overwrite.Instance, body : stream); enumerator++; window.RefreshData(enumerator, count); } } window.Close(); }
//这个函数可以被等待 public Task <List <string> > LoadFile(DirectoryInfo directory) { string[] type = { "*.jpg", "*.jpeg", "*.png", "*.pdf" }; IEnumerable <FileInfo> list = new List <FileInfo>(); foreach (var mode in type) { list = list.Union(directory.EnumerateFiles(mode)); } Console.WriteLine("正在拷贝文件,请稍后..."); int count = list.Count(); int complete = 0; List <string> func() { List <string> string_list = new List <string>(); foreach (var i in list) { //TODO 这个拷贝地址可能需要修改为用户能够自由指定 string path = pictureDir + i.Name; i.CopyTo(path); ++complete; CopyProgress?.Invoke((double)complete / count); string_list.Add(path); } Console.WriteLine("拷贝完成!"); return(string_list); } Task <List <string> > task = new Task <List <string> >(func); task.Start(); return(task); }
private async Task PerformCopy(bool remote, string path, Action <double, string> progress) { if (!remote) { Console.WriteLine($"Pushing local {path} to peer"); using (var stream = new FileStream(GetFullPath(path), FileMode.Open)) { var length = stream.Length; var position = 0L; var buffer = new byte[ChunkSize]; var timer = new CopyProgress(length); int got; while ((got = stream.Read(buffer, 0, buffer.Length)) != 0) { var mode = position == 0 ? "truncate" : "append"; var url = $"{_options.PeerServer}api/mirror/{mode}/{path}"; var content = new ByteArrayContent(buffer, 0, got); content.Headers.ContentType = new MediaTypeHeaderValue(OctetStream); var response = await Operations.Retry(50, () => Http.PutAsync(url, content)); if (!response.IsSuccessStatusCode) { throw new Exception($"{response.StatusCode} - {response.ReasonPhrase} [{url}]"); } position += got; progress((double)position / length, timer.Message(position)); } } } else { Console.WriteLine($"Pulling remote {path} from peer"); var length = long.Parse( await Operations.Retry(50, () => Http.GetStringAsync($"{_options.PeerServer}api/mirror/length/{path}") ) ); Console.WriteLine($"Remote {path} is {length} bytes"); var position = 0L; CreateParentFolders(path); using (var stream = new FileStream(GetFullPath(path), FileMode.Create)) { var timer = new CopyProgress(length); while (position < length) { var count = Math.Min(length, position + ChunkSize) - position; var url = $"{_options.PeerServer}api/mirror/pull/{position}/{count}/{path}"; var content = await Operations.Retry(50, () => Http.GetByteArrayAsync(url)); stream.Write(content, 0, content.Length); position += count; progress((double)position / length, timer.Message(position)); } } } Console.WriteLine("Finished file copy"); }
private void RaiseEvent(CopyProgress progress) { OnProgress?.Invoke(progress); progress.Log(); }