예제 #1
0
        protected override Object DoInBackground(params Object[] @params)
        {
            _imageDownloadData = @params[0].ToString();
            _fileName          = @params[1].ToString();

            try
            {
                if (_imageDownloadData.StartsWith("data:image"))
                {
                    var image = DataImage.TryParse(_imageDownloadData);
                    if (image == null)
                    {
                        return(false);
                    }
                    FileHelper.SaveImageAtDownloads(image.RawData, _fileName);
                    return(true);
                }

                if (!_imageDownloadData.StartsWith("https"))
                {
                    return(false);
                }

                DownloadMedia();
                return(true);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                return(false);
            }
        }
        public bool OnMenuItemClick(IMenuItem item)
        {
            if (string.IsNullOrEmpty(_itemExtra))
            {
                return(false);
            }

            var fileName = _itemExtra.StartsWith("data:image") ?
                           $"image.{DataImage.GetImageExtension(_itemExtra)}" :
                           URLUtil.GuessFileName(_itemExtra, null, null);

            if (FileHelper.MediaExists(fileName))
            {
                UserDialogs.Instance.ActionSheet(new ActionSheetConfig()
                                                 .SetTitle("An image with the same name already exists")
                                                 .SetMessage($"Do you want replace the existing {fileName} in download")
                                                 .Add(
                                                     "Replace file",
                                                     () => new ImageDownloader().Execute(_itemExtra, fileName))
                                                 .Add(
                                                     "Create new file",
                                                     () => new ImageDownloader().Execute(_itemExtra, FileHelper.GenerateNewFileName(fileName)))
                                                 .SetCancel()
                                                 .SetUseBottomSheet(true));
            }
            else
            {
                new ImageDownloader().Execute(_itemExtra, fileName);
            }

            return(true);
        }