Exemplo n.º 1
0
        public void DownloadFile(TLInt dcId, TLInputWebFileGeoPointLocation file, string fileName, TLObject owner, Action <DownloadableItem> callback)
        {
            var downloadableItem = GetDownloadableItem(dcId, file, fileName, owner, new TLInt(0), callback);

            downloadableItem.FileName = new TLString(fileName);
            lock (_itemsSyncRoot)
            {
                bool addFile = true;
                foreach (var item in _items)
                {
                    if (item.InputLocation.LocationEquals(file) &&
                        item.Owner == owner)
                    {
                        addFile = false;
                        break;
                    }
                }

                if (addFile)
                {
                    _items.Add(downloadableItem);
                }
            }

            StartAwaitingWorkers();
        }
Exemplo n.º 2
0
        protected DownloadableItem GetDownloadableItem(TLInt dcId, TLInputWebFileGeoPointLocation location, string fileName, TLObject owner, TLInt fileSize, Action <DownloadableItem> callback)
        {
            var item = new DownloadableItem
            {
                Owner         = owner,
                DCId          = dcId,
                Callback      = callback,
                InputLocation = location
            };

            item.Parts = GetItemParts(fileSize, item);

            return(item);
        }
Exemplo n.º 3
0
 public void DownloadFile(TLInt dcId, TLInputWebFileGeoPointLocation file, string fileName, TLObject owner)
 {
     DownloadFile(dcId, file, fileName, owner, null);
 }
Exemplo n.º 4
0
        public static BitmapSource ReturnOrEnqueueGeoPointImage(TLGeoPoint geoPoint, int width, int height, TLObject owner)
        {
            var destFileName = geoPoint.GetFileName();

            BitmapSource  imageSource;
            WeakReference weakImageSource;

            if (_cachedSources.TryGetValue(destFileName, out weakImageSource))
            {
                if (weakImageSource.IsAlive)
                {
                    imageSource = weakImageSource.Target as BitmapSource;

                    return(imageSource);
                }
            }

            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (!store.FileExists(destFileName))
                {
                    var config = IoC.Get <ICacheService>().GetConfig() as TLConfig82;
                    if (config == null)
                    {
                        return(null);
                    }

                    var fileManager = IoC.Get <IWebFileManager>();

                    var geoPoint82 = geoPoint as TLGeoPoint82;
                    var accessHash = geoPoint82 != null ? geoPoint82.AccessHash : new TLLong(0);

                    var inputLocation = new TLInputWebFileGeoPointLocation
                    {
                        GeoPoint = new TLInputGeoPoint {
                            Long = geoPoint.Long, Lat = geoPoint.Lat
                        },
                        AccessHash = accessHash,
                        W          = new TLInt(width),
                        H          = new TLInt(height),
                        Zoom       = new TLInt(15),
                        Scale      = new TLInt(2)
                    };

                    fileManager.DownloadFile(config.WebfileDCId, inputLocation, destFileName, owner, item =>
                    {
                        var messageMediaGeoPoint = owner as IMessageMediaGeoPoint;
                        if (messageMediaGeoPoint != null)
                        {
                            var newGeoPoint = messageMediaGeoPoint.Geo as TLGeoPoint;
                            if (newGeoPoint != null)
                            {
                                var newFileName = newGeoPoint.GetFileName();
                                var oldFileName = destFileName;

                                using (var store2 = IsolatedStorageFile.GetUserStoreForApplication())
                                {
                                    if (store2.FileExists(oldFileName))
                                    {
                                        if (!string.IsNullOrEmpty(oldFileName) && !string.Equals(oldFileName, newFileName, StringComparison.OrdinalIgnoreCase))
                                        {
                                            store2.CopyFile(oldFileName, newFileName, true);
                                            store2.DeleteFile(oldFileName);
                                        }
                                    }
                                }
                            }

                            Telegram.Api.Helpers.Execute.BeginOnUIThread(() =>
                            {
                                var messageMediaBase = owner as TLMessageMediaBase;
                                if (messageMediaBase != null)
                                {
                                    messageMediaBase.NotifyOfPropertyChange(() => messageMediaBase.Self);
                                }
                                var decryptedMessageMediaBase = owner as TLDecryptedMessageMediaBase;
                                if (decryptedMessageMediaBase != null)
                                {
                                    decryptedMessageMediaBase.NotifyOfPropertyChange(() => decryptedMessageMediaBase.Self);
                                }
                            });
                        }
                    });
                }
                else
                {
                    try
                    {
                        using (var stream = store.OpenFile(destFileName, FileMode.Open, FileAccess.Read))
                        {
                            stream.Seek(0, SeekOrigin.Begin);
                            var image = new BitmapImage();
                            image.SetSource(stream);
                            imageSource = image;
                        }

                        _cachedSources[destFileName] = new WeakReference(imageSource);
                    }
                    catch (Exception)
                    {
                        return(null);
                    }

                    return(imageSource);
                }
            }

            return(null);
        }