fix screenshot filenames with trailing / + update mp_splitext#17470
fix screenshot filenames with trailing / + update mp_splitext#17470kasper93 merged 5 commits intompv-player:masterfrom
Conversation
e39ee9d to
d4be69b
Compare
| video_file = "NO_FILE"; | ||
| char *name = mpctx->filename ? mp_basename_or_url(mpctx->filename) : "NO_FILE"; | ||
| if (mp_is_url(bstr0(name))) | ||
| name = mp_url_unescape(res, name); |
There was a problem hiding this comment.
I do not agree with this change at all. A good amount of urls will contain a useable filename at the end, e.g example.com/video_name.mp4.
Just because the handling of urls with trailing slashes was incorrect doesn't mean we should make things worse for the average case which don't have this problem.
There was a problem hiding this comment.
Most played URL are ytdl_hook URLs which don't have enough information in the basename.
There was a problem hiding this comment.
I guess it could be conditional on user-data/mpv/ytdl/json-subprocess-result being set?
Edit: or the last segment containing an extension/.?
There was a problem hiding this comment.
I guess it could be conditional on
user-data/mpv/ytdl/json-subprocess-resultbeing set?Edit: or the last segment containing an extension/.?
Or keep it as is and handle the off-case when there's a trailing slash (i.e basename returned empty string).
There was a problem hiding this comment.
Would be inconsistent with ${filename} though.
There was a problem hiding this comment.
Most played URL are ytdl_hook URLs which don't have enough information in the basename.
Even in the case of youtube, I'd much rather see watch?v=whatever.png as the filename (which already tells me it was a youtube video) than https:__www.youtube.com_watch?v=whatever.png. The latter is just useless noise being added for no reason.
If there's enough people who prefer the whole url, then we can consider adding it as some kind of option. But making it noisy under the guise of fixing a bug is something I consider a feature regression.
Would be inconsistent with ${filename} though.
True. Does it matter though?
There was a problem hiding this comment.
Well I suggested deprecating the formatters in favor of properties in #17021 (comment). And #10975 requested URL domains and paths in properties and track lines. So this gets messy, but whatever, updated to not take the basename only of paths ending with '/for%f`'.
There was a problem hiding this comment.
The latest change addresses my concern, but the commit message no longer correctly describes what the commit does.
d4be69b to
ca9fddd
Compare
DOCS/man/input.rst
Outdated
| unmodified filename.) | ||
| Currently played file, with path stripped, except URLs. The latter will be | ||
| returned in full with percent encoding undone. (The result is not | ||
| necessarily correct, but looks better for display purposes. Use the |
There was a problem hiding this comment.
What's "not necessarily correct" here? I'd assume this was referring to taking basename of urls, but since that's no longer being done, does this still apply?
misc/path_utils.h
Outdated
|
|
||
| /* Return pointer to last path segment or the original argument if it is a URL | ||
| */ | ||
| #define mp_basename_or_url(p) mp_is_url(bstr0(p)) ? (char *)p : mp_basename(p) |
There was a problem hiding this comment.
Wrap this in a paren just in case.
| #define mp_basename_or_url(p) mp_is_url(bstr0(p)) ? (char *)p : mp_basename(p) | |
| #define mp_basename_or_url(p) (mp_is_url(bstr0(p)) ? (char *)p : mp_basename(p)) |
Or better yet, just use a function which avoids precedence and/or double evaluation issue altogether. I don't really see any good reason for this to be a macro.
ed68100 to
6663f7f
Compare
|
Also applied the |
player/command.c
Outdated
| if (mp_is_url(bstr0(filename))) | ||
| mp_url_unescape_inplace(filename); | ||
| char *f = (char *)mp_basename(filename); | ||
| char *f = mp_basename_or_url(filename); |
There was a problem hiding this comment.
This should only be done for URLs with trailing slash to keep consistency with %f.
There was a problem hiding this comment.
I already noted that it's inconsistent. But then #10975 would not be fixed.
There was a problem hiding this comment.
It is documented that %{filename} is the same as %f.
But then #10975 would not be fixed.
OSC and terminal display can be fixed separately without changing ${filename} behavior.
There was a problem hiding this comment.
The OSC doesn't even use ${filename} but ${media-title} specified in the configurable title script-opt. How would you change that? Also the OSC is just an example reported here, e.g. the filename in stats has the same issue.
URL display would also still be very inconsistent since show-text ${playlist} and the playlist and history menus already show full URLs.
The inconsistency between %f and ${filename} is also bad for not allowing to deprecate the formatters. But even a new ${filename} sub-property wouldn't fix ${media-title}...
There was a problem hiding this comment.
The OSC doesn't even use
${filename}but${media-title}specified in the configurabletitlescript-opt. How would you change that?
But even a new${filename}sub-property wouldn't fix${media-title}...
Let ${media-title} use the new sub-property that keeps full url when there is no title. It can also do other processing on its own.
URL display would also still be very inconsistent since
show-text ${playlist}and the playlist and history menus already show full URLs.
They are not relevant to the topic here and these URLs also do not undo percent encoding. All of these were implemented after ${filename} and not designed for consistency to other properties.
The inconsistency between
%fand${filename}is also bad for not allowing to deprecate the formatters.
I would not deprecate the existing formatters and I am not seeing a consensus that they should be deprecated.
Also the initial implementation of ${filename} was 7ccf483 where a filename ending with a slash will have the full name displayed. In my opinion this fix should be done regardless of the existence of %f.
|
I'm not sure if It may or may not not be used in these places:
The playlist and history menus already takes the basename of non-URLs only. |
6663f7f to
b401941
Compare
|
Just removed |
URLs can end with trailing slashes (/) which in turn results in GNU
basename[1] returning the empty string, i.e. when the filename property
is queried. Instead use the whole path for everything that ends with /.
Subsequent path sanitation takes care of translating invalid path
component chars.
Issue reproduction steps:
```
mpv \
--screenshot-dir=$HOME/mpv-shots \
--screenshot-template='%f/%P' \
https://example.org/video/
```
This would result in %f expanding to '' and thus render
screenshot-template an absolute path which, for some reason, would in
turn take precedence of screenshot-dir and hence result in a
non-writeable path, i.e. `/timestamp.ext`.
With this new approach the resulting path looks like this:
`/home/user/mpv-shots/http:__example.org_video_/timestamp.ext`
[1] mpv-player#14635 (comment)
Co-authored-by: Guido Cella <[email protected]>
There is no reason to create the screenshot dir separately, because the final path starts with it. So defer creating directories to the last possible moment with all the information necessary.
b401941 to
49cfecc
Compare
The detection for leading dots was in the wrong place, in case path was not already a basename. Fixed by acting on basename. Also all leading dots are now getting skipped, otherwise the root of `path/to/...ext` could end up as `path/to/..` which in the wrong place/context would mean directory traversal. Additionally, since we act on the basename there is no longer a need to test for trailing path separators. The previous check also did not account for Windows paths with \ separators. Co-authored-by: Guido Cella <[email protected]>
There are at least two users of this functionality, the filename/no-ext property and the screenshot template %F specifier. The latter uses a buggy private version. So split out what the former does, so it can be reused. Co-authored-by: Guido Cella <[email protected]>
Reuse existing functionality, see previous commit introducing above function.
49cfecc to
209a2db
Compare
Updated version of #17021
WhitePeter's commits have the following modifications:
mp_splitextin a future PR, also making itstaticand the backend formp_strip_extand a newmp_get_ext)mp_strip_extalways allocate a new string as per N-R-K's suggestionI did not make
mp_basename_or_urlitself unescape URLs becausemp_basename_or_urlis useful inconfigfiles.cwhich probably shouldn't unescape URLs for watch later files