From 439c37539332781f976d4dc7dbdfa37f2b60a3bf Mon Sep 17 00:00:00 2001 From: eerielili Date: Sat, 28 Sep 2024 14:40:03 +0000 Subject: Store avatars in the user's cache directory. (#1621) * Store avatars in the user's cache directory. - Not anymore in ~/.local/share, where media files are stored. - Already existing ~/.local/share/dino/avatars directory will be moved to ~/.cache/dino/avatars - If both directories already exists, the old one (in ~/.local/share) is removed. * Simplify old-to-new-location logic --------- Co-authored-by: fiaxh --- libdino/src/service/avatar_manager.vala | 35 +++++++++++++++++++++++++++++++-- libdino/src/util/util.vala | 4 ++++ 2 files changed, 37 insertions(+), 2 deletions(-) (limited to 'libdino') diff --git a/libdino/src/service/avatar_manager.vala b/libdino/src/service/avatar_manager.vala index f99f37d4..192d5c9d 100644 --- a/libdino/src/service/avatar_manager.vala +++ b/libdino/src/service/avatar_manager.vala @@ -37,8 +37,39 @@ public class AvatarManager : StreamInteractionModule, Object { private AvatarManager(StreamInteractor stream_interactor, Database db) { this.stream_interactor = stream_interactor; this.db = db; - this.folder = Path.build_filename(Dino.get_storage_dir(), "avatars"); - DirUtils.create_with_parents(this.folder, 0700); + + File old_avatars = File.new_build_filename(Dino.get_storage_dir(), "avatars"); + File new_avatars = File.new_build_filename(Dino.get_cache_dir(), "avatars"); + this.folder = new_avatars.get_path(); + + // Move old avatar location to new one + if (old_avatars.query_exists()) { + if (!new_avatars.query_exists()) { + // Move old avatars folder (~/.local/share/dino) to new location (~/.cache/dino) + try { + new_avatars.get_parent().make_directory_with_parents(); + } catch (Error e) { } + try { + old_avatars.move(new_avatars, FileCopyFlags.NONE); + debug("Avatars directory %s moved to %s", old_avatars.get_path(), new_avatars.get_path()); + } catch (Error e) { } + } else { + // If both old and new folders exist, remove the old one + try { + FileEnumerator enumerator = old_avatars.enumerate_children("standard::*", FileQueryInfoFlags.NOFOLLOW_SYMLINKS); + FileInfo info = null; + while ((info = enumerator.next_file()) != null) { + FileUtils.remove(old_avatars.get_path() + "/" + info.get_name()); + } + DirUtils.remove(old_avatars.get_path()); + } catch (Error e) { } + } + } + + // Create avatar folder + try { + new_avatars.make_directory_with_parents(); + } catch (Error e) { } stream_interactor.account_added.connect(on_account_added); stream_interactor.module_manager.initialize_account_modules.connect((_, modules) => { diff --git a/libdino/src/util/util.vala b/libdino/src/util/util.vala index 553ebbfe..9f7ae45f 100644 --- a/libdino/src/util/util.vala +++ b/libdino/src/util/util.vala @@ -69,6 +69,10 @@ public static string get_storage_dir() { return Path.build_filename(Environment.get_user_data_dir(), "dino"); } +public static string get_cache_dir() { + return Path.build_filename(Environment.get_user_cache_dir(), "dino"); +} + [CCode (cname = "dino_gettext", cheader_filename = "dino_i18n.h")] public static extern unowned string _(string s); -- cgit v1.2.3-70-g09d2