コード例 #1
0
        public TLBitmapSource(TLWebDocument document)
        {
            _source = document;

            Phase = PHASE_FULL;

            var fileName = BitConverter.ToString(Utils.ComputeMD5(document.Url)).Replace("-", "") + ".jpg";

            if (File.Exists(FileUtils.GetTempFileName(fileName)))
            {
                _bitmapImage.UriSource = FileUtils.GetTempFileUri(fileName);
            }
            else
            {
                Execute.BeginOnThreadPool(async() =>
                {
                    var result = await _downloadWebFileManager.DownloadFileAsync(fileName, document.DCId, new TLInputWebFileLocation {
                        Url = document.Url, AccessHash = document.AccessHash
                    }, document.Size).AsTask(document.Download());
                    if (result != null && Phase <= PHASE_FULL)
                    {
                        Execute.BeginOnUIThread(() =>
                        {
                            _bitmapImage.UriSource = FileUtils.GetTempFileUri(fileName);
                        });
                    }
                });
            }
        }
コード例 #2
0
        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);
        }
コード例 #3
0
        public void CancelDownloadFile(TLWebDocument document)
        {
            Execute.BeginOnThreadPool(() =>
            {
                lock (_itemsSyncRoot)
                {
                    //var items = _items.Where(x => x.Owner == owner);
                    var items = _items.Where(x => x.InputWebFileLocation.AccessHash == document.AccessHash && x.InputWebFileLocation.Url == document.Url);

                    foreach (var item in items)
                    {
                        item.IsCancelled = true;
                    }
                }
            });
        }
コード例 #4
0
        public BitmapImage this[TLWebDocument document]
        {
            get
            {
                if (document == null)
                {
                    return(null);
                }

                if (_context.TryGetValue(document, out Tuple <TLBitmapSource, WeakReference <BitmapImage> > reference) &&
                    reference.Item2.TryGetTarget(out BitmapImage target))
                {
                    return(target);
                }

                var bitmap = new TLBitmapSource(document);
                _context[document] = new Tuple <TLBitmapSource, WeakReference <BitmapImage> >(bitmap, new WeakReference <BitmapImage>(bitmap.Image));
                return(bitmap.Image);
            }
        }
コード例 #5
0
ファイル: FileLoader.cs プロジェクト: Fart03/lau
        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;
                        }
                    }
                }
            });
        }
コード例 #6
0
ファイル: FileLoader.cs プロジェクト: Fart03/lau
        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);
                        }
                    }
                }
            });
        }
コード例 #7
0
ファイル: FileLoader.cs プロジェクト: Fart03/lau
 public void LoadFile(TLWebDocument document, bool force, bool cacheOnly, TaskCompletionSource <string> tsc)
 {
     LoadFile(null, document, null, null, 0, force, cacheOnly, tsc);
 }