55
66public class Music.AudioObject : Object {
77 public string uri { get ; construct; }
8- public Gdk . Texture ? texture { get ; set ; default = null ; }
9- public string album { get ; set ; }
10- public string artist { get ; set ; }
11- public string title { get ; set ; }
12- public int64 duration { get ; set ; default = 0 ; }
13- public string art_url { get ; set ; default = " " ; }
8+ public Gdk . Texture ? texture { get ; private set ; default = null ; }
9+ public string album { get ; private set ; }
10+ public string artist { get ; private set ; }
11+ public string title { get ; private set ; }
12+ public int64 duration { get ; private set ; default = 0 ; }
13+ public string art_url { get ; private set ; default = " " ; }
1414
1515 private static MetadataDiscoverer discoverer = new MetadataDiscoverer ();
1616
1717 public AudioObject (string uri ) {
18- Object (
19- uri: uri,
20- title: uri
21- );
18+ Object (uri: uri);
2219 }
2320
2421 construct {
22+ title = uri;
2523 discoverer. request (this );
2624 }
2725
@@ -44,12 +42,29 @@ public class Music.AudioObject : Object {
4442 artist = _(" Unknown" );
4543 }
4644
47- var sample = get_cover_sample (tag_list);
48- if (sample != null ) {
49- var buffer = sample. get_buffer ();
45+ string art_hash = uri;
46+ if (_artist != null && _album != null ) {
47+ art_hash = " %s :%s " . printf (_artist, _album);
48+ }
5049
51- if (buffer != null ) {
52- texture = Gdk . Texture . for_pixbuf (get_pixbuf_from_buffer (buffer));
50+ var art_file = File . new_for_path (Path . build_path (
51+ Path . DIR_SEPARATOR_S ,
52+ get_art_cache_dir (),
53+ Checksum . compute_for_string (SHA256 , art_hash)
54+ ));
55+
56+ if (art_file. query_exists ()) {
57+ art_url = art_file. get_uri ();
58+ texture = Gdk . Texture . from_file (art_file);
59+ } else {
60+ var sample = get_cover_sample (tag_list);
61+ if (sample != null ) {
62+ var buffer = sample. get_buffer ();
63+
64+ if (buffer != null ) {
65+ texture = Gdk . Texture . for_pixbuf (get_pixbuf_from_buffer (buffer));
66+ save_art_file. begin (texture, art_file);
67+ }
5368 }
5469 }
5570 }
@@ -97,6 +112,28 @@ public class Music.AudioObject : Object {
97112 return pix;
98113 }
99114
115+ private async void save_art_file (Gdk .Texture ? texture , File file ) requires (texture != null ) {
116+ try {
117+ DirUtils . create_with_parents (get_art_cache_dir (), 0755 );
118+
119+ var ostream = yield file. create_async (NONE );
120+ yield ostream. write_bytes_async (texture. save_to_png_bytes ());
121+
122+ art_url = file. get_uri ();
123+ } catch (Error e) {
124+ critical (" Error saving artwork file: %s " , e. message);
125+ }
126+ }
127+
128+ private string get_art_cache_dir () {
129+ return Path . build_path (
130+ Path . DIR_SEPARATOR_S ,
131+ Environment . get_user_cache_dir (),
132+ GLib . Application . get_default (). application_id,
133+ " art"
134+ );
135+ }
136+
100137 public static bool equal_func (AudioObject a , AudioObject b ) {
101138 return (a. uri == b. uri);
102139 }
0 commit comments