Esempio n. 1
0
        public void When_Uri_Is_Not_Absolute()
        {
            var uri = new Uri("local/test.txt", UriKind.Relative);

            Assert.ThrowsException <ArgumentOutOfRangeException>(
                () => AppDataUriEvaluator.ToPath(uri));
        }
Esempio n. 2
0
        internal void InitFromUri(Uri uri)
        {
            if (!uri.IsAbsoluteUri || uri.Scheme == "")
            {
                uri = new Uri(MsAppXScheme + ":///" + uri.OriginalString.TrimStart("/"));
            }

            if (uri.IsLocalResource())
            {
                InitFromResource(uri);
                return;
            }

            if (uri.IsAppData())
            {
                var filePath = AppDataUriEvaluator.ToPath(uri);
                InitFromFile(filePath);
            }

            if (uri.IsFile)
            {
                InitFromFile(uri.PathAndQuery);
            }

            WebUri = uri;
        }
Esempio n. 3
0
        public void When_Uri_Has_No_Path()
        {
            var uri = new Uri("ms-appdata:");

            Assert.ThrowsException <ArgumentOutOfRangeException>(
                () => AppDataUriEvaluator.ToPath(uri));
        }
Esempio n. 4
0
        public void When_Uri_Starts_With_Invalid_Folder()
        {
            var uri = new Uri($"ms-appdata:///space/file.png");

            Assert.ThrowsException <ArgumentOutOfRangeException>(
                () => AppDataUriEvaluator.ToPath(uri));
        }
Esempio n. 5
0
        public void When_Uri_Is_RoamingFolder_But_Has_Suffix()
        {
            var uri = new Uri("ms-appdata:///roamingfolder/");

            Assert.ThrowsException <ArgumentOutOfRangeException>(
                () => AppDataUriEvaluator.ToPath(uri));
        }
Esempio n. 6
0
        public void When_Uri_Has_Different_Scheme()
        {
            var uri = new Uri("ms-appx:///local/test.txt");

            Assert.ThrowsException <ArgumentOutOfRangeException>(
                () => AppDataUriEvaluator.ToPath(uri));
        }
Esempio n. 7
0
        private static NSUrl DecodeUri(Uri uri)
        {
            if (!uri.IsAbsoluteUri || uri.Scheme == "")
            {
                uri = new Uri(MsAppXScheme + ":///" + uri.OriginalString.TrimStart(new char[] { '/' }));
            }

            if (uri.IsLocalResource())
            {
                var file          = uri.PathAndQuery.TrimStart(new[] { '/' });
                var fileName      = Path.GetFileNameWithoutExtension(file);
                var fileExtension = Path.GetExtension(file)?.Replace(".", "");
                return(NSBundle.MainBundle.GetUrlForResource(fileName, fileExtension));
            }

            if (uri.IsAppData())
            {
                var filePath = AppDataUriEvaluator.ToPath(uri);
                return(NSUrl.CreateFileUrl(filePath, relativeToUrl: null));
            }

            if (uri.IsFile)
            {
                return(NSUrl.CreateFileUrl(uri.PathAndQuery, relativeToUrl: null));
            }

            return(new NSUrl(uri.ToString()));
        }
Esempio n. 8
0
        public void When_Double_Dot_Backs_Out_Above_App_Data()
        {
            var uri = new Uri("ms-appdata:///local/../hello/logo.png");

            Assert.ThrowsException <ArgumentOutOfRangeException>(
                () => AppDataUriEvaluator.ToPath(uri));
        }
Esempio n. 9
0
        private void SetVideoSource(Uri uri)
        {
            if (!uri.IsAbsoluteUri || uri.Scheme == "")
            {
                uri = new Uri(MsAppXScheme + ":///" + uri.OriginalString.TrimStart("/"));
            }

            if (uri.IsLocalResource())
            {
                var filename = global::System.IO.Path.GetFileName(uri.LocalPath);
                var afd      = Application.Context.Assets.OpenFd(filename);
                _player.SetDataSource(afd.FileDescriptor, afd.StartOffset, afd.Length);
                return;
            }

            if (uri.IsAppData())
            {
                var filePath = AppDataUriEvaluator.ToPath(uri);
                _player.SetDataSource(filePath);
                return;
            }

            if (uri.IsFile)
            {
                _player.SetDataSource(Application.Context, Android.Net.Uri.Parse(uri.PathAndQuery));
                return;
            }

            _player.SetDataSource(uri.ToString());
        }
Esempio n. 10
0
        public void When_Uri_Has_No_Subfolder_Specification()
        {
            var uri = new Uri("ms-appdata:///");

            Assert.ThrowsException <ArgumentOutOfRangeException>(
                () => AppDataUriEvaluator.ToPath(uri));
        }
Esempio n. 11
0
        public void When_Uri_Is_CacheFolder()
        {
            var uri = new Uri("ms-appdata:///temp/");

            var path = AppDataUriEvaluator.ToPath(uri);

            Assert.AreEqual(ApplicationData.Current.TemporaryFolder.Path, path);
        }
Esempio n. 12
0
        public void When_Uri_Is_RoamingFolder()
        {
            var uri = new Uri("ms-appdata:///roaming/");

            var path = AppDataUriEvaluator.ToPath(uri);

            Assert.AreEqual(ApplicationData.Current.RoamingFolder.Path, path);
        }
Esempio n. 13
0
        public void When_Uri_Is_LocalFolder_Without_Trailing_Slash()
        {
            var uri = new Uri("ms-appdata:///local");

            var path = AppDataUriEvaluator.ToPath(uri);

            Assert.AreEqual(ApplicationData.Current.LocalFolder.Path, path);
        }
Esempio n. 14
0
        public void When_Uri_Contains_Package_Name_But_Has_Suffix()
        {
            var packageName = Package.Current.Id.Name;
            var uri         = new Uri($"ms-appdata://{packageName}abcd/local/file.png");

            Assert.ThrowsException <ArgumentOutOfRangeException>(
                () => AppDataUriEvaluator.ToPath(uri));
        }
Esempio n. 15
0
        public void When_Uri_Contains_Plus_Sign()
        {
            var uri      = new Uri("ms-appdata:///local/+%23World.html");
            var expected = Path.Combine(ApplicationData.Current.LocalFolder.Path, "+#World.html");

            var path = AppDataUriEvaluator.ToPath(uri);

            Assert.AreEqual(expected, path);
        }
Esempio n. 16
0
        public void When_Double_Dot_Switch_To_Different_Root_Folder()
        {
            var uri      = new Uri("ms-appdata:///local/../roaming/test.png");
            var expected = Path.GetFullPath(Path.Combine(ApplicationData.Current.RoamingFolder.Path, "test.png"));

            var path = AppDataUriEvaluator.ToPath(uri);

            Assert.AreEqual(expected, path);
        }
Esempio n. 17
0
        public void When_Uri_Is_File_Nested_In_LocalFolder()
        {
            var subpath      = "path/tO/my/FilE123.txt";
            var uri          = new Uri($"ms-appdata:///local/{subpath}");
            var expectedPath = Path.GetFullPath(Path.Combine(ApplicationData.Current.LocalFolder.Path, subpath));

            var path = AppDataUriEvaluator.ToPath(uri);

            Assert.AreEqual(expectedPath, path);
        }
Esempio n. 18
0
        public void When_Uri_Contains_Package_Name()
        {
            var subpath     = "test/file.png";
            var packageName = Package.Current.Id.Name;
            var uri         = new Uri($"ms-appdata://{packageName}/local/{subpath}");
            var expected    = Path.GetFullPath(Path.Combine(ApplicationData.Current.LocalFolder.Path, subpath));

            var path = AppDataUriEvaluator.ToPath(uri);

            Assert.AreEqual(expected, path);
        }
Esempio n. 19
0
        public void When_Double_Dot_In_Scope_Of_LocalFolder()
        {
            var subpath          = "test/ok/../file.png";
            var shortenedSubpath = "test/file.png";

            var uri      = new Uri($"ms-appdata:///local/{subpath}");
            var expected = Path.GetFullPath(Path.Combine(ApplicationData.Current.LocalFolder.Path, shortenedSubpath));

            var path = AppDataUriEvaluator.ToPath(uri);

            Assert.AreEqual(expected, path);
        }
Esempio n. 20
0
        private void OnUriSourceChanged()
        {
            if (Parent == null)
            {
                // If the parent is not present we can wait for OnAttachedToWindow to call again.
                return;
            }

            ResetImage();

            if (!UriSource.HasValue())
            {
                return;
            }
            var newUri = new Uri(UriSource);

            if (newUri.Scheme == "resource" ||
                newUri.IsFile ||
                newUri.IsLocalResource())
            {
                SetImageResource(GetResourceId(newUri.PathAndQuery.TrimStart(new[] { '/' })));
            }
            else if (UriSource.StartsWith("res:///", StringComparison.OrdinalIgnoreCase))
            {
                int resourceId;

                if (int.TryParse(UriSource.Replace("res:///", ""), out resourceId))
                {
                    SetImageResource(resourceId);
                }
                else
                {
                    this.Log().WarnFormat("Failed to load asset resource [{0}]", UriSource);
                }
            }
            else if (UriSource.StartsWith("file://", StringComparison.OrdinalIgnoreCase) || newUri.IsAppData())
            {
                if (_targetHeight <= 0 || _targetWidth <= 0)
                {
                    _sourceUpdateRequired = true;
                    return;
                }

                var filePath = newUri.IsAppData() ?
                               AppDataUriEvaluator.ToPath(newUri) :
                               UriSource.TrimStart("file://", StringComparison.OrdinalIgnoreCase);

                var options = new BitmapFactory.Options();
                options.InJustDecodeBounds = true;
                BitmapFactory.DecodeFile(filePath, options).SafeDispose();
                var sampleSize = CalculateInSampleSize(options, _targetWidth, _targetHeight);

                options.InJustDecodeBounds = false;
#pragma warning disable CS0618 // Type or member is obsolete
                options.InPurgeable = true;
#pragma warning restore CS0618 // Type or member is obsolete
                options.InSampleSize = sampleSize;

                var bitmap = BitmapFactory.DecodeFile(filePath, options);

                if (_currentBitmap != bitmap)
                {
                    SetImageBitmap(bitmap);
                }
            }
            else
            {
                _download.Disposable = CoreDispatcher.Main
                                       .RunAsync(
                    CoreDispatcherPriority.Normal,
                    async(ct) =>
                {
                    var localUri = UriSource;

                    using (var b = await DownloadImage(localUri, _targetWidth, _targetHeight, ct))
                    {
                        if (!ct.IsCancellationRequested && b != null)
                        {
                            if (_currentBitmap != b)
                            {
                                SetImageBitmap(b);
                            }

                            if (this.Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Debug))
                            {
                                this.Log().DebugFormat("Bitmap set {0}", localUri);
                            }

                            if (_isInteractive)
                            {
                                _saveScale = 1f;
                                OnDrawableChanged();
                            }
                        }
                        else if (b == null && ImageLoader == null)
                        {
                            // The bitmap may be null if the image loader has already set the image from

                            this.Log().Warn("Bitmap is null after image download");
                        }
                    }
                }
                    );
            }
        }
Esempio n. 21
0
 public void When_Uri_Is_Null()
 {
     Assert.ThrowsException <ArgumentNullException>(
         () => AppDataUriEvaluator.ToPath(null));
 }