public FileLoadOperation(TLFileLocationBase photoLocation, String extension, int size) { this.state = stateIdle; /*if (photoLocation instanceof TL_fileEncryptedLocation) { * this.location = new TL_inputEncryptedFileLocation(); * this.location.id = photoLocation.volume_id; * this.location.volume_id = photoLocation.volume_id; * this.location.access_hash = photoLocation.secret; * this.location.local_id = photoLocation.local_id; * this.iv = new byte[32]; * System.arraycopy(photoLocation.iv, 0, this.iv, 0, this.iv.length); * this.key = photoLocation.key; * this.datacenter_id = photoLocation.dc_id; * } else*/ if (photoLocation is TLFileLocation fileLocation) { this.location = new TLInputFileLocation() { VolumeId = fileLocation.VolumeId, Secret = fileLocation.Secret, LocalId = fileLocation.LocalId }; this.datacenter_id = fileLocation.DCId; } this.currentType = FileType.Photo; this.totalBytesCount = size; if (extension == null) { extension = "jpg"; } this.ext = extension; }
private TLFile GetFile(TLInt dcId, TLInputFileLocationBase location, TLInt offset, TLInt limit) { var manualResetEvent = new ManualResetEvent(false); TLFile result = null; _mtProtoService.GetFileAsync(dcId, location, offset, limit, file => { result = file; manualResetEvent.Set(); }, error => { int delay; lock (_randomRoot) { delay = _random.Next(1000, 3000); } Execute.BeginOnThreadPool(TimeSpan.FromMilliseconds(delay), () => manualResetEvent.Set()); }); manualResetEvent.WaitOne(); return(result); }
private TLUploadFileBase GetFile(int dcId, TLInputFileLocationBase location, int offset, int limit) { var manualResetEvent = new ManualResetEvent(false); TLUploadFileBase result = null; _mtProtoService.GetFileAsync(dcId, location, offset, limit, file => { result = file; manualResetEvent.Set(); if (file is TLUploadFile full) { _statsService.IncrementReceivedBytesCount(_mtProtoService.NetworkType, _dataType, 4 + 4 + full.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 void GetFileAsync(TLInputFileLocationBase location, int offset, int limit, Action <TLUploadFileBase> callback, Action <TLRPCError> faultCallback = null) { var obj = new TLUploadGetFile { Location = location, Offset = offset, Limit = limit }; SendInformativeMessage("upload.getFile", obj, callback, faultCallback); }
public void GetFileAsync(int dcId, TLInputFileLocationBase location, int offset, int limit, Action <TLUploadFileBase> callback, Action <TLRPCError> faultCallback = null) { var obj = new TLUploadGetFile { Location = location, Offset = offset, Limit = limit }; const string caption = "upload.getFile"; SendInformativeMessage(caption, obj, callback, faultCallback, null, dcId, ConnectionType.Download, RequestFlag.ForceDownload | RequestFlag.FailOnServerError, true); }
private DownloadableItem GetDownloadableItem(TLInt dcId, TLInputFileLocationBase location, TLObject owner, TLInt fileSize) { var item = new DownloadableItem { Owner = owner, DCId = dcId, InputLocation = location }; item.Parts = GetItemParts(fileSize, item); return(item); }
private DownloadableItem GetDownloadableItem(TLString fileName, TLInt dcId, TLInputFileLocationBase location, TLObject owner, TLInt fileSize, Action <DownloadableItem> callback) { var item = new DownloadableItem { DCId = dcId, FileName = fileName, Owner = owner, InputLocation = location, Callback = callback }; item.Parts = GetItemParts(fileSize, item); return(item); }
protected void ProcessFilePart(DownloadablePart part, TLInt dcId, TLInputFileLocationBase location, out bool canceled) { do { TLRPCError error; TLFileBase result; if (part.ParentItem.CdnRedirect != null) { TLCdnFileReuploadNeeded cdnFileReuploadNeeded; bool tokenInvalid; result = GetCdnFile(part.ParentItem.CdnRedirect, part.Offset, part.Limit, out cdnFileReuploadNeeded, out error, out canceled, out tokenInvalid); if (cdnFileReuploadNeeded != null) { ReuploadFile(part.ParentItem.CdnRedirect, dcId, cdnFileReuploadNeeded.RequestToken, out error, out canceled, out tokenInvalid); } if (tokenInvalid) { lock (_itemsSyncRoot) { part.ParentItem.CdnRedirect = null; } continue; } } else { result = GetFile(dcId, location, part.Offset, part.Limit, out error, out canceled); var fileCdnRedirect = result as TLFileCdnRedirect; if (fileCdnRedirect != null) { lock (_itemsSyncRoot) { part.ParentItem.CdnRedirect = fileCdnRedirect; } continue; } } part.File = result as TLFile; if (canceled) { return; } } while (part.File == null); }
public void DownloadFileAsync(TLInt dcId, TLInputFileLocationBase fileLocation, TLObject owner, TLInt fileSize, Action <double> callback) { Execute.BeginOnThreadPool(() => { var downloadableItem = GetDownloadableItem(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 fileName = downloadableItem.InputLocation.GetFileName("video", ".mp4"); var getPartName = new Func <DownloadablePart, string>(x => x.ParentItem.InputLocation.GetPartFileName(x.Number, "video")); FileUtils.MergePartsToFile(getPartName, downloadableItem.Parts, fileName); downloadableItem.IsoFileName = fileName; _eventAggregator.Publish(downloadableItem); } else { var progress = downloadedCount / (double)count; callback.SafeInvoke(progress); lock (_itemsSyncRoot) { bool addFile = true; foreach (var item in _items) { if (item.InputLocation.LocationEquals(fileLocation) && item.Owner == owner) { addFile = false; break; } } if (addFile) { _items.Add(downloadableItem); } } StartAwaitingWorkers(); } }); }
public override bool LocationEquals(TLInputFileLocationBase location) { if (location == null) { return(false); } var fileLocation = location as TLInputVideoFileLocation; if (fileLocation == null) { return(false); } return (Id.Value == fileLocation.Id.Value && AccessHash.Value == fileLocation.AccessHash.Value); }
public override bool LocationEquals(TLInputFileLocationBase location) { if (location == null) { return(false); } var fileLocation = location as TLInputFileLocation; if (fileLocation == null) { return(false); } return (VolumeId.Value == fileLocation.VolumeId.Value && LocalId.Value == fileLocation.LocalId.Value && Secret.Value == fileLocation.Secret.Value); }
protected TLFileBase GetFile(TLInt dcId, TLInputFileLocationBase location, TLInt offset, TLInt limit, out TLRPCError er, out bool isCanceled) { var manualResetEvent = new ManualResetEvent(false); TLFileBase result = null; TLRPCError outError = null; var outIsCanceled = false; _mtProtoService.GetFileAsync(dcId, location, offset, limit, file => { result = file; manualResetEvent.Set(); }, error => { outError = error; if (error.CodeEquals(ErrorCode.INTERNAL) || (error.CodeEquals(ErrorCode.BAD_REQUEST) && (error.TypeEquals(ErrorType.LOCATION_INVALID) || error.TypeEquals(ErrorType.VOLUME_LOC_NOT_FOUND))) || (error.CodeEquals(ErrorCode.NOT_FOUND) && error.Message != null && error.Message.ToString().StartsWith("Incorrect dhGen"))) { outIsCanceled = true; manualResetEvent.Set(); return; } int delay; lock (_randomRoot) { delay = _random.Next(1000, 3000); } Execute.BeginOnThreadPool(TimeSpan.FromMilliseconds(delay), () => manualResetEvent.Set()); }); manualResetEvent.WaitOne(); er = outError; isCanceled = outIsCanceled; return(result); }
private static async void DecryptFile(TLSecureFile secureFile, TLInputFileLocationBase inputFileLocation, string localFileName, string previewFileName) { var fileSecret = Passport.DecryptValueSecret( secureFile.Secret, EnterPasswordViewModel.Secret, secureFile.FileHash); var encryptedFile = await ApplicationData.Current.LocalFolder.GetFileAsync(inputFileLocation.GetFileName("document")); var decryptedTuple = await Passport.DecryptFile(localFileName, encryptedFile, fileSecret, secureFile.FileHash); var stream = await decryptedTuple.Item1.OpenReadAsync(); await DialogDetailsViewModel.ResizeJpeg(stream, 180, localFileName, previewFileName); Telegram.Api.Helpers.Execute.BeginOnUIThread(() => { secureFile.NotifyOfPropertyChange(() => secureFile.Self); }); }
public FileLoadOperation(TLDocument documentLocation) { try { /*if (documentLocation instanceof TLRPC.TL_documentEncrypted) { * location = new TLRPC.TL_inputEncryptedFileLocation(); * location.id = documentLocation.id; * location.access_hash = documentLocation.access_hash; * datacenter_id = documentLocation.dc_id; * iv = new byte[32]; * System.arraycopy(documentLocation.iv, 0, iv, 0, iv.length); * key = documentLocation.key; * } else*/ if (documentLocation is TLDocument document) { location = new TLInputDocumentFileLocation { Id = document.Id, AccessHash = document.AccessHash }; datacenter_id = document.DCId; } totalBytesCount = documentLocation.Size; if (key != null) { int toAdd = 0; if (totalBytesCount % 16 != 0) { bytesCountPadding = 16 - totalBytesCount % 16; totalBytesCount += bytesCountPadding; } } ext = FileLoader.getDocumentFileName(documentLocation); int idx; if (ext == null || (idx = ext.LastIndexOf('.')) == -1) { ext = ""; } else { ext = ext.Substring(idx); } if ("audio/ogg".Equals(documentLocation.MimeType)) { currentType = FileType.Audio; } else if ("video/mp4".Equals(documentLocation.MimeType)) { currentType = FileType.Video; } else { currentType = FileType.File; } if (ext.Length <= 1) { if (documentLocation.MimeType != null) { switch (documentLocation.MimeType) { case "video/mp4": ext = ".mp4"; break; case "audio/ogg": ext = ".ogg"; break; default: ext = ""; break; } } else { ext = ""; } } } catch (Exception e) { //FileLog.e(e); onFail(true, 0); } }
public void DownloadFileAsync(TLString originalFileName, TLInt dcId, TLInputFileLocationBase fileLocation, TLObject owner, TLInt fileSize, Action <double> startCallback, Action <DownloadableItem> callback = null) { Execute.BeginOnThreadPool(() => { var downloadableItem = GetDownloadableItem(originalFileName, dcId, fileLocation, owner, fileSize, callback); var downloadedCount = downloadableItem.Parts.Count(x => x.Status == PartStatus.Processed); var count = downloadableItem.Parts.Count; var isComplete = downloadedCount == count; if (isComplete) { var fileExtension = Path.GetExtension(downloadableItem.FileName.ToString()); var fileName = downloadableItem.InputLocation.GetFileName("document", fileExtension); Func <DownloadablePart, string> getPartName = x => downloadableItem.InputLocation.GetPartFileName(x.Number, "document"); FileUtils.MergePartsToFile(getPartName, downloadableItem.Parts, fileName); downloadableItem.IsoFileName = fileName; _eventAggregator.Publish(downloadableItem); } else { var progress = downloadedCount / (double)count; startCallback.SafeInvoke(progress); lock (_itemsSyncRoot) { bool addFile = true; foreach (var item in _items) { if (item.InputLocation.LocationEquals(fileLocation)) { //item.SuppressMerge = true; if (callback != null) { if (item.Callbacks == null) { item.Callbacks = new List <Action <DownloadableItem> >(); } item.Callbacks.Add(callback); addFile = false; break; } //item. if (item.Owner == owner) { addFile = false; break; } } } if (addFile) { _items.Add(downloadableItem); } } StartAwaitingWorkers(); } }); }
public abstract bool LocationEquals(TLInputFileLocationBase location);