private TLUploadWebFile GetFile(int dcId, TLInputWebFileLocation location, int offset, int limit) { var manualResetEvent = new ManualResetEvent(false); TLUploadWebFile result = null; _mtProtoService.GetWebFileAsync(dcId, location, offset, limit, file => { result = file; manualResetEvent.Set(); _statsService.IncrementReceivedBytesCount(_mtProtoService.NetworkType, _dataType, 4 + 4 + file.Bytes.Length + 4); }, error => { int delay; lock (_randomRoot) { delay = _random.Next(1000, 3000); } Execute.BeginOnThreadPool(TimeSpan.FromMilliseconds(delay), () => manualResetEvent.Set()); }); manualResetEvent.WaitOne(); return(result); }
public FileLoadOperation(TLWebDocument webDocument) { this.state = 0; this.webLocation = new TLInputWebFileLocation(); this.webLocation.Url = webDocument.Url; this.webLocation.AccessHash = webDocument.AccessHash; this.totalBytesCount = webDocument.Size; this.datacenter_id = webDocument.DCId; String defaultExt = FileLoader.getExtensionByMime(webDocument.MimeType); if (webDocument.MimeType.StartsWith("image/")) { this.currentType = FileType.Photo; } else if (webDocument.MimeType.Equals("audio/ogg")) { this.currentType = FileType.Audio; } else if (webDocument.MimeType.StartsWith("video/")) { this.currentType = FileType.Video; } else { this.currentType = FileType.File; } this.ext = ImageLoader.getHttpUrlExtension(webDocument.Url, defaultExt); }
public void DownloadFile(string originalFileName, int dcId, TLInputWebFileLocation fileLocation, TLObject owner, int fileSize) { Execute.BeginOnThreadPool(() => { var downloadableItem = GetDownloadableItem(originalFileName, dcId, fileLocation, owner, fileSize); var downloadedCount = downloadableItem.Parts.Count(x => x.Status == PartStatus.Processed); var count = downloadableItem.Parts.Count; var isComplete = downloadedCount == count; if (isComplete) { //var id = downloadableItem.InputDocumentLocation.Id; //var accessHash = downloadableItem.InputDocumentLocation.AccessHash; var fileExtension = Path.GetExtension(downloadableItem.FileName.ToString()); var fileName = GetFileName(downloadableItem.InputWebFileLocation, fileExtension); //string.Format("document{0}_{1}{2}", id, accessHash, fileExtension); Func <DownloadablePart, string> getPartName = x => downloadableItem.InputWebFileLocation.GetPartFileName(x.Number); //string.Format("document{0}_{1}_{2}.dat", id, accessHash, x.Number); FileUtils.MergePartsToFile(getPartName, downloadableItem.Parts, fileName); downloadableItem.IsoFileName = fileName; _eventAggregator.Publish(downloadableItem); } else { var progress = downloadedCount / (double)count; Execute.BeginOnThreadPool(() => _eventAggregator.Publish(new DownloadProgressChangedEventArgs(downloadableItem, progress))); lock (_itemsSyncRoot) { bool addFile = true; foreach (var item in _items) { if (item.InputWebFileLocation.AccessHash == fileLocation.AccessHash && item.InputWebFileLocation.Url == fileLocation.Url) { //item.SuppressMerge = true; //item. if (item.Owner == owner) { Execute.ShowDebugMessage("Cancel document=" + fileLocation.Url); addFile = false; break; } } } if (addFile) { _items.Add(downloadableItem); } } StartAwaitingWorkers(); } }); }
public void GetWebFileAsync(int dcId, TLInputWebFileLocation location, int offset, int limit, Action <TLUploadWebFile> callback, Action <TLRPCError> faultCallback = null) { var obj = new TLUploadGetWebFile { Location = location, Offset = offset, Limit = limit }; const string caption = "upload.getWebFile"; SendInformativeMessage(caption, obj, callback, faultCallback, null, dcId, ConnectionType.Download, RequestFlag.ForceDownload | RequestFlag.FailOnServerError, true); }
private DownloadableItem GetDownloadableItem(string fileName, int dcId, TLInputWebFileLocation location, TLObject owner, int fileSize) { var item = new DownloadableItem { DCId = dcId, FileName = fileName, Owner = owner, InputWebFileLocation = location }; item.Parts = GetItemParts(fileSize, item); return(item); }
public IAsyncOperationWithProgress <DownloadableItem, double> DownloadFileAsync(string originalFileName, int dcId, TLInputWebFileLocation fileLocation, int fileSize) { return(AsyncInfo.Run <DownloadableItem, double>((token, progress) => { var tsc = new TaskCompletionSource <DownloadableItem>(); var downloadableItem = GetDownloadableItem(originalFileName, dcId, fileLocation, null, fileSize); downloadableItem.Callback = tsc; downloadableItem.Progress = progress; var downloadedCount = downloadableItem.Parts.Count(x => x.Status == PartStatus.Processed); var count = downloadableItem.Parts.Count; var isComplete = downloadedCount == count; if (isComplete) { //var id = downloadableItem.InputDocumentLocation.Id; //var accessHash = downloadableItem.InputDocumentLocation.AccessHash; var fileExtension = Path.GetExtension(downloadableItem.FileName.ToString()); var fileName = GetFileName(downloadableItem.InputWebFileLocation, fileExtension); //string.Format("document{0}_{1}{2}", id, accessHash, fileExtension); Func <DownloadablePart, string> getPartName = x => downloadableItem.InputWebFileLocation.GetPartFileName(x.Number); //string.Format("document{0}_{1}_{2}.dat", id, accessHash, x.Number); FileUtils.MergePartsToFile(getPartName, downloadableItem.Parts, fileName); downloadableItem.IsoFileName = fileName; downloadableItem.Progress.Report(1.0); downloadableItem.Callback.TrySetResult(downloadableItem); } else { downloadableItem.Progress.Report(downloadedCount / (double)count); lock (_itemsSyncRoot) { bool addFile = true; foreach (var item in _items) { if (item.InputWebFileLocation.AccessHash == fileLocation.AccessHash && item.InputWebFileLocation.Url == fileLocation.Url) { downloadableItem.Callback = item.Callback; downloadableItem.Progress = item.Progress; addFile = false; Debug.WriteLine("Already downloading document"); //item.SuppressMerge = true; //item. //if (item.Owner == owner) //{ // Execute.ShowDebugMessage("Cancel document=" + fileLocation.Id); // addFile = false; // break; //} } } if (addFile) { _items.Add(downloadableItem); } } StartAwaitingWorkers(); } return tsc.Task; })); }
public static string GetFileName(TLInputWebFileLocation fileLocation, string fileExtension) { return(BitConverter.ToString(Utils.ComputeMD5(fileLocation.Url)).Replace("-", "") + fileExtension); }