コード例 #1
0
        private async void ImportHandler()
        {
            var fileExt    = Path.GetExtension(TargetFileName);
            var sourceFile = await PlatformService.SelectFileAsync(new string[] { fileExt });

            if (sourceFile == null)
            {
                return;
            }

            var md5 = await CryptographyService.ComputeMD5Async(sourceFile);

            if (md5.ToLowerInvariant() != TargetMD5.ToLowerInvariant())
            {
                var title   = LocalizationService.GetLocalizedString(FileHashMismatchTitleKey);
                var message = LocalizationService.GetLocalizedString(FileHashMismatchMessageKey);
                await DialogsService.AlertAsync(message, title);

                return;
            }

            using (var inStream = await sourceFile.OpenAsync(PCLStorage.FileAccess.Read))
            {
                var targetFile = await TargetFolder.CreateFileAsync(targetFileName, CreationCollisionOption.ReplaceExisting);

                using (var outStream = await targetFile.OpenAsync(PCLStorage.FileAccess.ReadAndWrite))
                {
                    await inStream.CopyToAsync(outStream);

                    await outStream.FlushAsync();
                }

                FileAvailable = true;
            }
        }
コード例 #2
0
        private async Task <bool> SaveFileFromPhotoDetails(Photo photo)
        {
            if (photo.Details != null && photo.Details.CanDownload)
            {
                try
                {
                    var    originalPhoto = photo.Details.Sizes.FirstOrDefault(p => p.Label.ToLower() == "original");
                    string photoFormat   = ".jpg";

                    if (!String.IsNullOrEmpty(photo.Format))
                    {
                        photoFormat = "." + photo.Format;
                    }

                    foreach (char c in System.IO.Path.GetInvalidFileNameChars())
                    {
                        photo.Title = photo.Title.Replace(c, '_');
                    }

                    StorageFile photoFile = null;

                    // Look for a year / month folder for the target folder to download into
                    if (photo.Dates.DateTaken != null && UseFolderStructure)
                    {
                        var yearFolder = await TargetFolder.CreateFolderAsync(photo.Dates.DateTaken.Year.ToString(), CreationCollisionOption.OpenIfExists);

                        var monthFolder = await yearFolder?.CreateFolderAsync(photo.Dates.DateTaken.Month.ToString(), CreationCollisionOption.OpenIfExists);

                        photoFile = await monthFolder?.CreateFileAsync(photo.Title + "_" + photo.Secret + photoFormat, Windows.Storage.CreationCollisionOption.ReplaceExisting);
                    }

                    if (photoFile == null)
                    {
                        photoFile = await TargetFolder?.CreateFileAsync(photo.Title + "_" + photo.Secret + photoFormat, Windows.Storage.CreationCollisionOption.ReplaceExisting);
                    }

                    if (originalPhoto != null && photoFile != null)
                    {
                        HttpClient client = new HttpClient();
                        Debug.WriteLine("Downloading:");
                        Debug.WriteLine(originalPhoto.Source);
                        byte[] buffer = await client.GetByteArrayAsync(originalPhoto.Source); // Download file

                        using (Stream stream = await photoFile.OpenStreamForWriteAsync())
                        {
                            stream.Write(buffer, 0, buffer.Length); // Save
                        }
                        return(true);
                    }
                }
                catch
                {
                    return(false);
                }
            }

            return(false);
        }