Exemplo n.º 1
0
        public async Task Save()
        {
            if (!Directory.Exists(Root))
            {
                Directory.CreateDirectory(Root);
            }
            File.WriteAllText(Path.Combine(Root, "syncurr.user.json"), JsonConvert.SerializeObject(this));
            bool me = (await ImgurHelper.GetToken()).AccountId == Id;

            if (!me && !Properties.Settings.Default.Users.Contains(Root))
            {
                Properties.Settings.Default.Users.Add(Root);
                Properties.Settings.Default.Save();
            }
        }
Exemplo n.º 2
0
        protected override async Task DoSync(ProgressDialogController pdc)
        {
            if (Me?.Name == null)
            {
                if (User.Exists(Properties.Settings.Default.MeRoot))
                {
                    Me = User.Get(Properties.Settings.Default.MeRoot);
                }
                if (Me?.Name == null)
                {
                    IOAuth2Token token = await ImgurHelper.GetToken();

                    Me = User.Create(Properties.Settings.Default.MeRoot, token.AccountUsername, token.AccountId);
                }
            }
            if (Me?.Name != null)
            {
                IsLoading = true;
                try
                {
                    pdc.SetMessage(string.Format("synchronizing account\n\n{0}\n{1}", Me.Name, Me.Root));
                    await Me.Sync(this);
                }
                catch (Exception ex)
                {
                    List <string> msgs = new List <string>();
                    while (ex != null)
                    {
                        msgs.Add(ex.Message);
                        ex = ex.InnerException;
                    }
                    await DialogCoordinator.Instance.ShowMessageAsync(this, "Failed to synchronize albums", string.Join("\n\n", msgs));
                }
                IsLoading = false;
            }
        }
Exemplo n.º 3
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();
            });
        }
Exemplo n.º 4
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);
                }
            });
        }