Repository: aome510/spotify-player (language: rust)

Issue:
OS error: file name too long
**Describe the bug**
When playing a song from a playlist that contains very long authors or song name, spotify_player runs into problems attempting to save files that have names which exceed the (mac) OS length limit for writing.

**To Reproduce**
On MacOS sonoma, play a song from an album such as [this one](https://open.spotify.com/album/16zfntcR7VAJWn9Ovooo1w?si=D1L7rw4lTE2fbOsFq-fjvg). I did this on a separate chrome spotify client, before trying to launch the terminal. Now that the song is active, the terminal will produce this output:
```
$ spotify_player
Error: Failed to initialize the Spotify data

Caused by:
    File name too long (os error 63)
```

**Expected behaviour**
The CLI doesn't attempt to write files which have names that exceed the mac limit. Perhaps they should be clipped, or encoded, etc.

**Log and backtrace**
No backtrace necessary, (my backtrace is entirely empty, this is my first launch of spotify_player). Here is the relevant log context:
```
2024-07-14T17:17:50.192136Z  INFO spotify_player::streaming: Initializing a new integrated player with device_id=c563f912-f32a-42fe-b29e-b6c585ea8eda
2024-07-14T17:17:50.192260Z  INFO spotify_player::streaming: Starting an integrated Spotify player using librespot's spirc protocol
2024-07-14T17:17:50.542336Z  INFO spotify_player::client: Retrieving image from url: https://i.scdn.co/image/ab67616d0000b273bfd2db0a9adc1f642860d277
2024-07-14T17:17:50.714032Z  INFO spotify_player::client: Saving the retrieved image into /Users/user/.cache/spotify-player/image/Love Songs - Piano Transcriptions Without Words-Angela Hewitt, Franz Liszt, Robert Schumann, Franz Schubert, Richard Strauss, Christoph Willibald Gluck, Gottfried Heinrich Stölzel, Gustav Mahler, Edvard Grieg, Gabriel Fauré, Manuel de Falla, George Gershwin, Percy Grainger-cover.jpg
```

It's clear from above what is causing the long file error.

**Environment**
- OS: Macos Sonoma 14.5
- Application version: 0.18.2



Discussion / hints:
It seems the max length for MacOS file names is 255 characters. [1]

Do all titles have a - before artist names? Maybe we can truncate there?

I think the main concern is having potential file name conflicts. I'm not familiar enough with this player to know how it handles this or whether that even matters.

[1] APFS: https://en.wikipedia.org/wiki/Comparison_of_file_systems#Limits
> Do all titles have a - before artist names? Maybe we can truncate there?
> 
> I think the main concern is having potential file name conflicts. I'm not familiar enough with this player to know how it handles this or whether that even matters.

Yeah, we can truncate and the main concern is file name conflict. One simple way to fix this issue to only include first artist's name instead of all artists.

Fix patch:
diff --git a/spotify_player/src/client/mod.rs b/spotify_player/src/client/mod.rs
index d4e62ed8..d911bd8b 100644
--- a/spotify_player/src/client/mod.rs
+++ b/spotify_player/src/client/mod.rs
@@ -1415,13 +1415,15 @@ impl Client {
             None => return Ok(()),
         };
 
-        let path = (format!(
-            "{}-{}-cover.jpg",
+        let filename = format!(
+            "{}-{}-cover-{}.jpg",
             track.album.name,
-            crate::utils::map_join(&track.album.artists, |a| &a.name, ", ")
-        ))
+            track.album.artists.first().unwrap().name,
+            // first 6 characters of the album's id
+            &track.album.id.as_ref().unwrap().id()[..6]
+        )
         .replace('/', ""); // remove invalid characters from the file's name
-        let path = configs.cache_folder.join("image").join(path);
+        let path = configs.cache_folder.join("image").join(filename);
 
         if configs.app_config.enable_cover_image_cache {
             self.retrieve_image(url, &path, true).await?;


Test patch: