예제 #1
0
        public async Task <Stream> LoadImageAsync(string path)
        {
            async Task <Stream> GetImageNotFound()
            {
                Stream notFoundStream = null;
                await Task.Run(() => notFoundStream = typeof(EncryptorAssistant).Assembly.GetManifestResourceStream("Vault.Core.Resources.ImageNotFound.png")).ConfigureAwait(true);

                return(notFoundStream);
            }

            Stream imageStream = new MemoryStream();

            if (File.Exists(path))
            {
                using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
                {
                    try
                    {
                        await EncryptorAssistant.GetEncryptor().DecryptAsync(fs, imageStream).ConfigureAwait(true);

                        imageStream.Position = 0;
                    }
                    catch (Exception ex)
                    {
                        App.LogError("Could not decrypt image", ex);
                        imageStream = await GetImageNotFound().ConfigureAwait(true);
                    }
                }
            }
            else
            {
                imageStream = await GetImageNotFound().ConfigureAwait(true);
            }
            return(imageStream);
        }
예제 #2
0
        public async Task <bool> TryExportMediaAsync(Media item, string outputPath)
        {
            AesHmacEncryptor encryptor = EncryptorAssistant.GetEncryptor();

            // Check that the file to import exists
            if (!File.Exists(item.FilePath))
            {
                Log.Information("Export - Could not locate the image to export");
                ShowErrorDialog("There was an error exporting the file. Please try again. It may be that the file is corrupt and cannot be exported.");
                return(false);
            }

            try
            {
                using (FileStream fs = new FileStream(item.FilePath, FileMode.Open, FileAccess.Read))
                    using (FileStream imageOutput = new FileStream(outputPath, FileMode.CreateNew, FileAccess.ReadWrite))
                    {
                        await encryptor.DecryptAsync(fs, imageOutput).ConfigureAwait(false);
                    }
                return(true);
            } catch (Exception ex)
            {
                App.LogError("Error exporting media", ex);
                ShowErrorDialog("There was an error exporting the file. Please try again. It may be that the file is corrupt and cannot be exported.");
                return(false);
            }
        }
예제 #3
0
        public async Task <bool> TryUpdateMediaAsync(string path, Stream stream)
        {
            using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite))
            {
                try
                {
                    stream.Position = 0;
                    await EncryptorAssistant.GetEncryptor().EncryptAsync(stream, fs).ConfigureAwait(false);

                    return(true);
                }
                catch (Exception ex)
                {
                    App.LogError("Could not update media", ex);
                    return(false);
                }
            }
        }
예제 #4
0
        public async Task <Media> TryImportImageAsync(string path)
        {
            AesHmacEncryptor encryptor = EncryptorAssistant.GetEncryptor();
            Media            media     = null;

            bool success = await Task.Run(() =>
            {
                // Check that the image store folder exists, and try to create one if not
                if (!CheckDirectoryExists(IMAGE_FOLDER_PATH))
                {
                    return(false);
                }

                // Check that the file to import exists
                if (!File.Exists(path))
                {
                    Log.Information("Import - Could not locate the image to import");
                    ShowErrorDialog("Could not locate the file to import. Please check it is not being used by any other programs");
                    return(false);
                }

                string errMessage = string.Empty;
                try
                {
                    // Create the new media store
                    int id = RealmHelpers.GetNextId <Media>();
                    media  = new Media(id, MediaType.Image)
                    {
                        FilePath  = GetFilePath(IMAGE_FOLDER_PATH, id),
                        Name      = Path.GetFileName(path),
                        ThumbPath = GetThumbPath(IMAGE_FOLDER_PATH, id)
                    };

                    // Load, encrypt and save the file and thumbnail
                    using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
                        using (FileStream imageOutput = new FileStream(media.FilePath, FileMode.CreateNew, FileAccess.ReadWrite))
                            using (FileStream thumbOutput = new FileStream(media.ThumbPath, FileMode.CreateNew, FileAccess.ReadWrite))
                                using (Image image = Image.Load(fs))
                                    using (MemoryStream imageStore = new MemoryStream())
                                        using (MemoryStream thumbStore = new MemoryStream())
                                        {
                                            // Find the thumbnail scalar
                                            double scalar;
                                            if (image.Width < image.Height)
                                            {
                                                scalar = MAX_THUMB_SIZE / image.Height;
                                            }
                                            else
                                            {
                                                scalar = MAX_THUMB_SIZE / image.Width;
                                            }

                                            // Save the image as a PNG
                                            image.SaveAsPng(imageStore);
                                            imageStore.Position = 0;
                                            encryptor.EncryptAsync(imageStore, imageOutput);

                                            // Mutate and encrypt the thumbnail
                                            image.Mutate(x => x.Resize((int)(image.Width * scalar), (int)(image.Height * scalar)));
                                            image.SaveAsPng(thumbStore);
                                            thumbStore.Position = 0;
                                            encryptor.EncryptAsync(thumbStore, thumbOutput).Wait();
                                        }
                }
                catch (FileNotFoundException fnfex)
                {
                    errMessage = "Could not locate the file to import. Please try again!";
                    App.LogError("Error importing image", fnfex);
                }
                catch (UnauthorizedAccessException uaex)
                {
                    errMessage = "You don't have access to that file, sorry!";
                    App.LogError("Error importing image", uaex);
                }
                catch (Exception ex)
                {
                    errMessage = "An error occured while importing. Please try again!";
                    App.LogError("Error importing image", ex);
                }

                if (!string.IsNullOrEmpty(errMessage))
                {
                    ShowErrorDialog(errMessage);
                    return(false);
                }
                else
                {
                    return(true);
                }
            }).ConfigureAwait(true);

            // Add the media to the realm if required
            if (success)
            {
                Realm realm = RealmHelpers.GetRealmInstance();
                await realm.WriteAsync((r) => r.Add(media)).ConfigureAwait(true);

                return(media);
            }
            else
            {
                return(null);
            }
        }