devsnd / cherrymusic

Stream your own music collection to all your devices! The easy to use free and open-source music streaming server.
http://www.fomori.org/cherrymusic
GNU General Public License v3.0
1.03k stars 187 forks source link

First Letter of Folders disappearing #374

Closed marky1991 closed 10 years ago

marky1991 commented 10 years ago

Relevant info from previous ticket:

Interestingly, now my folders are missing the first character, e.g. from the logs: (If this is not related to this fix, let me know and I'll move this to a seperate ticket)

[131114-01:11] Fetching album art for keywords ' wesome Songs'
[131114-01:11] Fetching album art for keywords ' rt History'
[131114-01:11] Fetching album art for keywords ' nime'
[131114-01:11] Fetching album art for keywords ' odcasts de Español'
[131114-01:11] Fetching album art for keywords ' nglish'
[131114-01:11] Fetching album art for keywords ' avoritas'

As a result of that, I can't play view files through "Browse Files" anymore. From the logs:

[131114-01:15] ERROR   : [14/Nov/2013:01:15:08] HTTP Traceback (most recent call last):
  File "/usr/local/lib/python3.3/dist-packages/cherrypy/_cprequest.py", line 656, in respond
    response.body = self.handler()
  File "/usr/local/lib/python3.3/dist-packages/cherrypy/lib/encoding.py", line 188, in __call__
    self.body = self.oldhandler(*args, **kwargs)
  File "/usr/local/lib/python3.3/dist-packages/cherrypy/_cpdispatch.py", line 34, in __call__
    return self.callable(*self.args, **self.kwargs)
  File "/home/mark/Documentos/cherrymusic-devel/cherrymusicserver/httphandler.py", line 266, in api
    return json.dumps({'data': handler(**handler_args)})
  File "/home/mark/Documentos/cherrymusic-devel/cherrymusicserver/httphandler.py", line 402, in api_listdir
    return [entry.to_dict() for entry in self.model.listdir(directory)]
  File "/home/mark/Documentos/cherrymusic-devel/cherrymusicserver/cherrymodel.py", line 104, in listdir
    allfilesindir = os.listdir(absdirpath)
FileNotFoundError: [Errno 2] No such file or directory: '/home/mark/Música/spañol'

I'm reliably getting this error. It appears to somehow go away a while after the server is restarted.

Still trying to figure out what makes it start working properly.

marky1991 commented 10 years ago

Aha! After waiting a long time, nothing happened. After doing a search, however, the letters suddenly returned. After doing another restart of the server, the letters were gone again. After another search, they came back.

tilboerner commented 10 years ago

Alright! If you switch back to Browse after searching, are the letters gone again?

marky1991 commented 10 years ago

No, after searching, they stay until the next server restart.

devsnd commented 10 years ago

Well, that is some mysterious :shit: going on there.

So, these are all the places where we cut strings. We can check them one after another and ✓ them...

[cherrymusic]$ grep -PRn "\[\d:" ./cherrymusicserver/*
marky1991 commented 10 years ago

I've discovered the issue.

The root cause was that in my config file, I specified my basepath like this:

basedir = /home/mark/Música/

, with a trailing slash. Looking at the comments in the configuration file, this is indeed not exactly as specified. (But the difference is so tiny that I as a user (or a developer, on that note) would never expect this to cause an issue.)

In cherrymodel.py, strippath does this:

if path.startswith(cherry.config['media.basedir']):
    return path[len(cherry.config['media.basedir']) + 1:]

which assumes that media.basedir doesn't have a trailing slash. If there is a trailing slash, it removes the first character from the rest of the path, giving the faulty behavior I noticed. (How it ends up working after doing a search, I don't know. I didn't bother further looking into the code.)

I'm feeling too lazy to bother forking the project for one little fix, but if I were going to fix the code, I'd do something like this:

def strippath(path):
    if path.startswith(cherry.config['media.basedir']):
        return os.path.relpath(path, cherry.config['media.basedir'])
    return path

With this modification, everything works properly with or without the trailing slash. (I've verified that this function is available for all the versions of python that y'all support.)

As far as I can tell, no other code uses the limiting assumption, so this should be the only trouble spot.

devsnd commented 10 years ago

Thanks for your investigation and you fix! I've commited the fix to the devel branch.

Have a few funny icons as reward! :dancers: :octopus: :8ball: :monkey:

tilboerner commented 10 years ago

Good job tracking that one down. I like the use of relpath there, very neat.