From a3504ae9f8300757cab1d1641d25b5809d70c7bf Mon Sep 17 00:00:00 2001
From: Blopware <blopware@blopware.com>
Date: Thu, 29 Apr 2021 19:22:33 +0200
Subject: [PATCH] Sanitize remote tracks' saving locations with slashes on
 their names

For that case, the resulting saved file made use of these slashes to
store the tracks in subdirectories of federation_cache/xx/xx/xx, instead of
in the federation_cache/xx/xx/xx directory itself. As a consequence, the
tracks are downloaded in the wrong location, and upon any trial of
playing the tracks, funkwhale responds with the message
"The track cannot be loaded. You can have connectivity issues"

An example of a podcast with this kind of RSS items is located in
https://www.rtve.es/api/programas/2082/audios.rss

This commit overcomes this by a simple replacement of the offending
slashes to hyphens.
---
 api/funkwhale_api/music/models.py | 3 +++
 changes/changelog.d/1435.bug      | 1 +
 2 files changed, 4 insertions(+)
 create mode 100644 changes/changelog.d/1435.bug

diff --git a/api/funkwhale_api/music/models.py b/api/funkwhale_api/music/models.py
index 4325a16970..197a8fc1c9 100644
--- a/api/funkwhale_api/music/models.py
+++ b/api/funkwhale_api/music/models.py
@@ -688,6 +688,8 @@ TRACK_FILE_IMPORT_STATUS_CHOICES = (
     ("skipped", "Skipped"),
 )
 
+def sanitize_remote_location_path(filename):
+    return filename.replace("/", "-")
 
 def get_file_path(instance, filename):
     if isinstance(instance, UploadVersion):
@@ -696,6 +698,7 @@ def get_file_path(instance, filename):
     if instance.library.actor.get_user():
         return common_utils.ChunkedPath("tracks")(instance, filename)
     else:
+        filename = sanitize_remote_location_path(filename)
         # we cache remote tracks in a different directory
         return common_utils.ChunkedPath("federation_cache/tracks")(instance, filename)
 
diff --git a/changes/changelog.d/1435.bug b/changes/changelog.d/1435.bug
new file mode 100644
index 0000000000..7329f8b4a2
--- /dev/null
+++ b/changes/changelog.d/1435.bug
@@ -0,0 +1 @@
+Sanitize remote tracks' saving locations with slashes on their names (#1435)
\ No newline at end of file
-- 
GitLab