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 void LoadFile(TLDocument document, TLWebDocument webDocument, TLFileLocationBase location, String locationExt, int locationSize, bool force, bool cacheOnly, TaskCompletionSource <string> tsc) { //fileLoaderQueue.postRunnable(new Runnable() { //@Override //public void run() Execute.BeginOnThreadPool(() => { String fileName = null; if (location != null) { fileName = getAttachFileName(location, locationExt); } else if (document != null) { fileName = getAttachFileName(document); } else if (webDocument != null) { fileName = getAttachFileName(webDocument); } if (fileName == null || fileName.Contains("" + int.MinValue)) { return; } loadOperationPaths.TryGetValue(fileName, out FileLoadOperation operation); if (operation != null) { if (force) { operation.setForceRequest(true); List <FileLoadOperation> downloadQueue; if (TLMessage.isVoiceDocument(document) || TLMessage.isVoiceWebDocument(webDocument)) { downloadQueue = audioLoadOperationQueue; } else if (location != null || TLMessage.isImageWebDocument(webDocument)) { downloadQueue = photoLoadOperationQueue; } else { downloadQueue = loadOperationQueue; } if (downloadQueue != null) { int index = downloadQueue.IndexOf(operation); if (index > 0) { downloadQueue.RemoveAt(index); downloadQueue.Insert(0, operation); } } } return; } string tempDir = GetDirectory(MEDIA_DIR_CACHE); string storeDir = tempDir; int type = MEDIA_DIR_CACHE; if (location != null) { operation = new FileLoadOperation(location, locationExt, locationSize); type = MEDIA_DIR_IMAGE; } else if (document != null) { operation = new FileLoadOperation(document); if (TLMessage.isVoiceDocument(document)) { type = MEDIA_DIR_AUDIO; } else if (TLMessage.isVideoDocument(document)) { type = MEDIA_DIR_VIDEO; } else { type = MEDIA_DIR_DOCUMENT; } } else if (webDocument != null) { operation = new FileLoadOperation(webDocument); if (TLMessage.isVoiceWebDocument(webDocument)) { type = MEDIA_DIR_AUDIO; } else if (TLMessage.isVideoWebDocument(webDocument)) { type = MEDIA_DIR_VIDEO; } else if (TLMessage.isImageWebDocument(webDocument)) { type = MEDIA_DIR_IMAGE; } else { type = MEDIA_DIR_DOCUMENT; } } if (!cacheOnly) { storeDir = GetDirectory(type); } operation.SetPaths(storeDir, tempDir); String finalFileName = fileName; int finalType = type; // FileLoadOperation.FileLoadOperationDelegate fileLoadOperationDelegate = new FileLoadOperation.FileLoadOperationDelegate() { // @Override // public void didFinishLoadingFile(FileLoadOperation operation, File finalFile) // { // if (delegate != null) // { // delegate.fileDidLoaded(finalFileName, finalFile, finalType); // } // checkDownloadQueue(document, webDocument, location, finalFileName); // } // @Override // public void didFailedLoadingFile(FileLoadOperation operation, int reason) // { // checkDownloadQueue(document, webDocument, location, finalFileName); // if (delegate != null) // { // delegate.fileDidFailedLoad(finalFileName, reason); // } // } // @Override // public void didChangedLoadProgress(FileLoadOperation operation, float progress) // { // if (delegate != null) // { // delegate.fileLoadProgressChanged(finalFileName, progress); // } // } //}; //operation.setDelegate(fileLoadOperationDelegate); operation.DidChangedLoadProgress = (s, args) => Debug.WriteLine("Download progress: " + args); operation.DidFailedLoadingFile = (s, args) => checkDownloadQueue(document, webDocument, location, finalFileName); operation.DidFinishLoadingFile = (s, args) => { checkDownloadQueue(document, webDocument, location, finalFileName); tsc.SetResult(args.FullName); }; /*if (location != null) { * operation = new FileLoadOperation(location.dc_id, location.volume_id, location.volume_id, location.secret, location.local_id, location.key, location.iv, locationExt != null ? locationExt : "jpg", 0, locationSize, !cacheOnly ? getDirectory(type) : tempDir, tempDir, fileLoadOperationDelegate); * } else if (document != null) { * String ext = FileLoader.getDocumentFileName(document); * int idx; * if (ext == null || (idx = ext.lastIndexOf('.')) == -1) { * ext = ""; * } else { * ext = ext.substring(idx + 1); * } * if (ext.length() <= 0) { * if (document.mime_type != null) { * switch (document.mime_type) { * case "video/mp4": * ext = "mp4"; * break; * case "audio/ogg": * ext = "ogg"; * break; * default: * ext = ""; * break; * } * } else { * ext = ""; * } * } * operation = new FileLoadOperation(document.dc_id, document.id, 0, document.access_hash, 0, document.key, document.iv, ext, document.version, document.size, !cacheOnly ? getDirectory(type) : tempDir, tempDir, fileLoadOperationDelegate); * }*/ loadOperationPaths[fileName] = operation; int maxCount = force ? 3 : 1; if (type == MEDIA_DIR_AUDIO) { if (currentAudioLoadOperationsCount < maxCount) { if (operation.Start()) { currentAudioLoadOperationsCount++; } } else { if (force) { audioLoadOperationQueue.Insert(0, operation); } else { audioLoadOperationQueue.Add(operation); } } } else if (location != null) { if (currentPhotoLoadOperationsCount < maxCount) { if (operation.Start()) { currentPhotoLoadOperationsCount++; } } else { if (force) { photoLoadOperationQueue.Insert(0, operation); } else { photoLoadOperationQueue.Add(operation); } } } else { if (currentLoadOperationsCount < maxCount) { if (operation.Start()) { currentLoadOperationsCount++; } } else { if (force) { loadOperationQueue.Insert(0, operation); } else { loadOperationQueue.Add(operation); } } } }); }
private void checkDownloadQueue(TLDocument document, TLWebDocument webDocument, TLFileLocationBase location, String arg1) { //fileLoaderQueue.postRunnable(new Runnable() { //@Override //public void run() //{ Execute.BeginOnThreadPool(() => { loadOperationPaths.TryRemove(arg1, out FileLoadOperation operation); if (TLMessage.isVoiceDocument(document) || TLMessage.isVoiceWebDocument(webDocument)) { if (operation != null) { if (operation.WasStarted()) { currentAudioLoadOperationsCount--; } else { audioLoadOperationQueue.Remove(operation); } } while (audioLoadOperationQueue.Count > 0) { operation = audioLoadOperationQueue[0]; int maxCount = operation.IsForceRequest() ? 3 : 1; if (currentAudioLoadOperationsCount < maxCount) { operation = audioLoadOperationQueue.Poll(); if (operation != null && operation.Start()) { currentAudioLoadOperationsCount++; } } else { break; } } } else if (location != null || TLMessage.isImageWebDocument(webDocument)) { if (operation != null) { if (operation.WasStarted()) { currentPhotoLoadOperationsCount--; } else { photoLoadOperationQueue.Remove(operation); } } Debug.WriteLine(arg1 + " Completed"); while (photoLoadOperationQueue.Count > 0) { operation = photoLoadOperationQueue[0]; int maxCount = operation.IsForceRequest() ? 3 : 1; if (currentPhotoLoadOperationsCount < maxCount) { operation = photoLoadOperationQueue.Poll(); if (operation != null && operation.Start()) { currentPhotoLoadOperationsCount++; Debug.WriteLine(arg1 + " New download"); } } else { break; } } } else { if (operation != null) { if (operation.WasStarted()) { currentLoadOperationsCount--; } else { loadOperationQueue.Remove(operation); } } while (loadOperationQueue.Count > 0) { operation = loadOperationQueue[0]; int maxCount = operation.IsForceRequest() ? 3 : 1; if (currentLoadOperationsCount < maxCount) { operation = loadOperationQueue.Poll(); if (operation != null && operation.Start()) { currentLoadOperationsCount++; } } else { break; } } } }); }
public void LoadFile(TLFileLocationBase location, String ext, int size, bool cacheOnly, TaskCompletionSource <string> tsc) { LoadFile(null, null, location, ext, size, true, cacheOnly || size == 0 /*|| (location != null && location.key != null)*/, tsc); }