Пример #1
0
        public async Task Upload(Album album)
        {
            ImageEndpoint endpoint = new ImageEndpoint(await ImgurHelper.GetClient());

            using (Stream src = File.OpenRead(Path.Combine(album.Root, FileName)))
            {
                await endpoint.UploadImageStreamAsync(src, album.Id, Name);
            }
        }
Пример #2
0
        public async Task Sync(object context)
        {
            if (!Synchronize)
            {
                return;
            }
            await Task.Run(async() =>
            {
                // get local files
                FileInfo[] local = new DirectoryInfo(Root).GetFiles().Where(it => new string[] { ".jpg", ".gif", ".png", ".bmp" }.Contains(it.Extension.ToLowerInvariant())).ToArray();

                // get remote files
                AlbumEndpoint endpoint = new AlbumEndpoint(await ImgurHelper.GetClient());
                ImgurAlbum             = await endpoint.GetAlbumAsync(Id);
                bool owned             = (await ImgurHelper.GetToken()).AccountId == ImgurAlbum.AccountId.ToString();

                // (A) filter for files only in local (not in remote)
                FileInfo[] onlyLocal = local.Where(l => !ImgurAlbum.Images.Select(r => new Image(r)).Any(r => r.FileName == l.Name)).ToArray();
                // (B) filter for files only in remote (not in local)
                Image[] onlyRemote = ImgurAlbum.Images.Select(r => new Image(r)).Where(r => !local.Any(l => r.FileName == l.Name)).ToArray();

                if (owned)                 //own album
                {
                    // upload files (A) where not in json
                    FileInfo[] upload = onlyLocal.Where(l => !Images.Any(i => i == l.Name)).ToArray();
                    foreach (FileInfo img in upload)
                    {
                        Image image = new Image();
                        image.Name  = Path.GetFileNameWithoutExtension(img.Name);
                        image.Link  = img.Name;
                        await image.Upload(this);
                    }
                    // download files (B) where not in json
                    Image[] download = onlyRemote.Where(r => !Images.Any(i => i == r.FileName)).ToArray();
                    foreach (Image img in download)
                    {
                        await img.Download(this);
                    }
                    // delete files (A) from local where in json
                    if (Properties.Settings.Default.DeleteLocalImage)
                    {
                        FileInfo[] deleteLocal = onlyLocal.Where(l => Images.Any(i => i == l.Name)).ToArray();
                        foreach (FileInfo img in deleteLocal)
                        {
                            bool ok = true;
                            if (Properties.Settings.Default.AskDeleteLocalImage)
                            {
                                MessageDialogResult result = await DialogCoordinator.Instance.ShowMessageAsync(context, "Delete local image?", img.FullName, MessageDialogStyle.AffirmativeAndNegative);
                                ok = result == MessageDialogResult.Affirmative;
                            }
                            if (ok)
                            {
                                File.Delete(img.FullName);
                            }
                        }
                    }
                    // delete files (B) from remote where in json
                    if (Properties.Settings.Default.DeleteRemoteImage)
                    {
                        Image[] deleteRemote = onlyRemote.Where(r => Images.Any(i => i == r.FileName)).ToArray();
                        foreach (Image img in deleteRemote)
                        {
                            bool ok = true;
                            if (Properties.Settings.Default.AskDeleteRemoteImage)
                            {
                                MessageDialogResult result = await DialogCoordinator.Instance.ShowMessageAsync(context, "Delete Imgur image?", img.Name, MessageDialogStyle.AffirmativeAndNegative);
                                ok = result == MessageDialogResult.Affirmative;
                            }
                            if (ok)
                            {
                                await img.Delete();
                            }
                        }
                    }
                }
                else                 //not own album
                {
                    // remove files (A)
                    if (Properties.Settings.Default.DeleteLocalImage)
                    {
                        foreach (FileInfo img in onlyLocal)
                        {
                            bool ok = true;
                            if (Properties.Settings.Default.AskDeleteLocalImage)
                            {
                                MessageDialogResult result = await DialogCoordinator.Instance.ShowMessageAsync(context, "Delete local image?", img.FullName, MessageDialogStyle.AffirmativeAndNegative);
                                ok = result == MessageDialogResult.Affirmative;
                            }
                            if (ok)
                            {
                                File.Delete(img.FullName);
                            }
                        }
                    }
                    // download files (B)
                    foreach (Image img in onlyRemote)
                    {
                        await img.Download(this);
                    }
                }

                // update json
                string[] images = new DirectoryInfo(Root).GetFiles().Where(it => new string[] { ".jpg", ".gif", ".png", ".bmp" }.Contains(it.Extension.ToLowerInvariant())).Select(it => it.Name).ToArray();

                App.Current.Dispatcher.Invoke((Action) delegate
                {
                    Images.Clear();
                    foreach (string img in images)
                    {
                        Images.Add(img);
                    }
                });
                Save();
            });
        }
Пример #3
0
        public async Task Sync(object context)
        {
            await Task.Run(async() =>
            {
                // get account
                AccountEndpoint endpoint = new AccountEndpoint(await ImgurHelper.GetClient());
                ImgurUser = await endpoint.GetAccountAsync(Name);
                bool me   = (await ImgurHelper.GetToken()).AccountId == ImgurUser.Id.ToString();

                // get local albums
                Album[] local = new DirectoryInfo(Root).GetDirectories().Where(it => it.Name[0] != '.').Select(it => Album.Get(it.FullName, "", null, true)).ToArray();

                // get remote albums
                IAlbum[] remote = (await endpoint.GetAlbumsAsync(Name)).ToArray();

                // (A) filter for albums with id only in local (not in remote)
                Album[] onlyLocal = local.Where(l => l.Id != null && !remote.Any(r => r.Id == l.Id)).ToArray();

                // (B) filter for albums only in remote (not in local)
                IAlbum[] onlyRemote = remote.Where(r => !local.Any(l => l.Id == r.Id)).ToArray();

                if (me)
                {
                    // download albums (B) where not in json
                    IAlbum[] download = onlyRemote.Where(r => !Albums.Any(a => a.Id == r.Id)).ToArray();
                    foreach (IAlbum album in download)
                    {
                        await Album.Get(Root, album.Title ?? album.Id, album.Id, true).Sync(context);
                    }
                    // delete albums (A) from local where in json
                    if (Properties.Settings.Default.DeleteLocalFolder)
                    {
                        Album[] deleteLocal = onlyLocal.Where(l => Albums.Any(a => a.Id == l.Id && a.Synchronize)).ToArray();
                        foreach (Album album in deleteLocal)
                        {
                            bool ok = true;
                            if (Properties.Settings.Default.AskDeleteLocalFolder)
                            {
                                MessageDialogResult result = await DialogCoordinator.Instance.ShowMessageAsync(context, "Delete local album?", album.Root, MessageDialogStyle.AffirmativeAndNegative);
                                ok = result == MessageDialogResult.Affirmative;
                            }
                            if (ok)
                            {
                                Directory.Delete(album.Root, true);
                            }
                        }
                    }
                    // delete albums (B) from remote where in json
                    if (Properties.Settings.Default.DeleteRemoteFolder)
                    {
                        IAlbum[] deleteRemote = onlyRemote.Where(r => Albums.Any(a => a.Id == r.Id && a.Synchronize)).ToArray();
                        foreach (IAlbum album in deleteRemote)
                        {
                            bool ok = true;
                            if (Properties.Settings.Default.AskDeleteRemoteFolder)
                            {
                                MessageDialogResult result = await DialogCoordinator.Instance.ShowMessageAsync(context, "Delete Imgur album?", album.Title, MessageDialogStyle.AffirmativeAndNegative);
                                ok = result == MessageDialogResult.Affirmative;
                            }
                            if (ok)
                            {
                                await endpoint.DeleteAlbumAsync(album.Id, Name);
                            }
                        }
                    }
                    // create albums without id
                    Album[] upload = local.Where(l => l.Id == null && l.Synchronize).ToArray();
                    foreach (Album album in upload)
                    {
                        AlbumEndpoint ep = new AlbumEndpoint(await ImgurHelper.GetClient());
                        IAlbum rAlbum    = await ep.CreateAlbumAsync(album.Title);
                        album.ImgurAlbum = rAlbum;
                        await album.Sync(context);
                    }
                }
                else
                {
                    // remove albums (A)
                    if (Properties.Settings.Default.DeleteLocalFolder)
                    {
                        foreach (Album album in onlyLocal.Where(it => it.Synchronize))
                        {
                            bool ok = true;
                            if (Properties.Settings.Default.AskDeleteLocalFolder)
                            {
                                MessageDialogResult result = await DialogCoordinator.Instance.ShowMessageAsync(context, "Delete local album?", album.Root, MessageDialogStyle.AffirmativeAndNegative);
                                ok = result == MessageDialogResult.Affirmative;
                            }
                            if (ok)
                            {
                                album.Remove();
                            }
                        }
                    }
                    // download albums (B)
                    foreach (IAlbum album in onlyRemote)
                    {
                        await Album.Get(Root, album.Title ?? album.Id, album.Id, true).Sync(context);
                    }
                }

                // udpate json
                AlbumRoots     = new DirectoryInfo(Root).GetDirectories().Where(it => it.Name[0] != '.').Select(it => it.FullName).ToList();
                Album[] albums = AlbumRoots.Select(it => Album.Get(it, "")).ToArray();

                App.Current.Dispatcher.Invoke((Action) delegate
                {
                    Albums.Clear();
                    foreach (Album album in albums)
                    {
                        Albums.Add(album);
                    }
                });
                await Save();

                // synchronize album images
                foreach (Album album in albums)
                {
                    await album.Sync(context);
                }
            });
        }
Пример #4
0
 public async Task Delete()
 {
     ImageEndpoint endpoint = new ImageEndpoint(await ImgurHelper.GetClient());
     await endpoint.DeleteImageAsync(Id);
 }