From 252aa31b1124cc6d76f431756c1b9e364844ade3 Mon Sep 17 00:00:00 2001
From: Eliot Berriot <contact@eliotberriot.com>
Date: Sun, 8 Jul 2018 10:31:21 +0200
Subject: [PATCH] Fix #138: Raise a warning instead of crashing when getting a
 broken path in file import

---
 .../management/commands/import_files.py       | 23 ++++++++++++++++++-
 changes/changelog.d/138.bugfix                |  1 +
 2 files changed, 23 insertions(+), 1 deletion(-)
 create mode 100644 changes/changelog.d/138.bugfix

diff --git a/api/funkwhale_api/providers/audiofile/management/commands/import_files.py b/api/funkwhale_api/providers/audiofile/management/commands/import_files.py
index b59c0046f..2aba0c145 100644
--- a/api/funkwhale_api/providers/audiofile/management/commands/import_files.py
+++ b/api/funkwhale_api/providers/audiofile/management/commands/import_files.py
@@ -82,10 +82,31 @@ class Command(BaseCommand):
         try:
             for import_path in options["path"]:
                 matching += glob.glob(import_path, **glob_kwargs)
-            matching = sorted(list(set(matching)))
+            raw_matching = sorted(list(set(matching)))
         except TypeError:
             raise Exception("You need Python 3.5 to use the --recursive flag")
 
+        matching = []
+        for m in raw_matching:
+            # In some situations, the path is encoded incorrectly on the filesystem
+            # so we filter out faulty paths and display a warning to the user.
+            # see https://code.eliotberriot.com/funkwhale/funkwhale/issues/138
+            try:
+                m.encode("utf-8")
+                matching.append(m)
+            except UnicodeEncodeError:
+                try:
+                    previous = matching[-1]
+                except IndexError:
+                    previous = None
+                self.stderr.write(
+                    self.style.WARNING(
+                        "[warning] Ignoring undecodable path. Previous ok file was {}".format(
+                            previous
+                        )
+                    )
+                )
+
         if options["in_place"]:
             self.stdout.write(
                 "Checking imported paths against settings.MUSIC_DIRECTORY_PATH"
diff --git a/changes/changelog.d/138.bugfix b/changes/changelog.d/138.bugfix
new file mode 100644
index 000000000..5dab279b4
--- /dev/null
+++ b/changes/changelog.d/138.bugfix
@@ -0,0 +1 @@
+Raise a warning instead of crashing when getting a broken path in file import (#138)
-- 
GitLab