Problem/Motivation
Using tokens under File name that may have slashes in them ('/') causes File (Field) Paths to create the subdirectories as provided by the token. If you have a file name with this token:
[node:title]-[file:ffp-name-only-original].[file:ffp-extension-original]
and create a node with the title "sub/dir" and upload a file name 'example.png' File (Field) Paths will then place the file in the following directory structure:
files/sub/dir-example.png
This is not the expected behavior when operating with the file name of a file, but as @Deciphered pointed out in #15 changing this might break existing installations.
Proposed resolution
Provide an option under both File path options and File name options that adds a special case for the path delimiter with a default vaule that won't break existing behavior, but will allow file names to reside in the directory they were configured.
Remaining tasks
Provide a working patch
User interface changes
An additional checkbox under both File path options and File name options that allows for special treatment of '/'.
API changes
None.
Original report by @pp
Original title: If token contains slash (/) module doesn't work correctly
How reproduce this bug:
- drush dl filefield_paths entity_api
- drush en filefield_paths entity_api entity_token
- Add a new vocabulary call test_vocabulary
- Add 3 term:
term_1
-term_2
--term_3
(term_2 parent of term_3, term_1 parent of term_2) - Add a new content type call test
- Add a taxonomy term reference field call test_tax_ref
- Add file filed and set fielfield Path settings a following:
[node:field-test_tax_ref:parents:join-path] - Add a new test content. Set test_tax_ref to term_3. Upload a file, and save node.
The file path will be "term_1term_2/[filename]" instead of "term_1/term_2/[filename]"
Comment | File | Size | Author |
---|---|---|---|
#22 | filefield_paths-remove-slash-option-1942720-22.patch | 3.31 KB | kaare |
#18 | option_to_remove_slashes-1942720-18.patch | 1.74 KB | BWPanda |
#1 | 1942720.patch | 668 bytes | pp |
Comments
Comment #1
pp CreditAttribution: pp commentedHere is the patch which resolve this issue.
Comment #2
Deciphered CreditAttribution: Deciphered commentedThanks for the work, committed.
Comment #4
kaareIt still doesn't work due to this in filefield_paths.module:383:
Whenever a token e.g. [node:title] contains a '/', filefield_paths will create new folders according to node title, so if you have a node title "very/long/subdirectory", pathauto won't get a chance to clean this up and you end up with a very deep folder structure. I suspect you could do all kinds of wrong if you start using this for node title: "../../index.php" or worse. token_replace() needs to be run on each individual
$path
within the for-loop to make sure it's the folder structure the site admin has configured that will rule, rather than what an unknowing user inputs.Folder separation should happen before token replacement, like this:
Patch attached.
Comment #5
kaareNope. That wasn't enough. There is also this:
So if [node:title] is
"You Cant See Me/This Will Be Processed.ButNotThis"
, only"This Will Be Processed"
will be sent to the cleaners. The replaced path will still have unprocessed parts of the file name with potential directory delimiters.This new patch rebuilds the file name based on everything returned from
pathinfo()
and it also keeps #2047835: "Cleanup using Pathauto" Not Working Properly happy.The question is whether users want to use tokens with an existing directory structure, but that would require additional configuration in the field form in the line of
[ ] Keep directory structure from tokens
. But I imagine this is a followup issue.Comment #6
kaareAaand, pathinfo() sets 'dirname' to '.' when only file name is present which resulted in files starting with '.' (hidden). Added test for that too.
Comment #7
kaareSteps to reproduce after clean install with 'standard' profile:
admin/structure/types/manage/article/fields/field_image
[node:title].[file:ffp-extension-original]
and enable 'Cleanup using Pathauto' under 'File Name Options'.a/b/c/d
and upload a random image..../files/images/a/b/c/d.jpg
.../files/images/abcd.jpg
, as expected.This is kind of critical, so it would be nice if someone could confirm and/or review it.
Comment #8
Deciphered CreditAttribution: Deciphered commented@kaare,
Please note that this original issue is that the user wanted slashes in tokens not to be removed, and your issue is that you do want slashes in tokens to be removed. So what you see as a critical issue, others see as a feature, and therefore I am in a very difficult situation, to fix your issue I have to break things for others.
So what is the solution? I'm open to suggestions.
In the meantime, you could always make custom tokens that sanitize the node title as you see fit. Not ideal, but this issue does need further thought before it can proceed.
Comment #9
Deciphered CreditAttribution: Deciphered commentedAlso, this issue, and my last reply, inspired a new module; Rule Tokens.
It allows you to use a Rule component to process the value of a token creating a new token. So you could theoretically create a new token called something like [node:title:rule_tokens:rules_remove_slashes] that removed all slashes from the [node:title] token just using Rules for the logic.
Comment #10
kaareOh, I'm so sorry. Totally misinterpreted that. I totally see you being torn by this. '/' is the essence of this module, and how to interpret that when part of tokens is tough.
Though, my expectation on this is:
file_name
part should not be given a chance to change the directory structure. It's unexpected. You configure the file name and expect it to reside in the directory configured infile_path
....['file_name']['options']['pathauto']
(Clean up using pathauto) is set, pathauto should orchestrate manipulation of the individual characters, including '/'. At least forfile_name
So my suggestion would be to let tokens values orchestrate the directory structure in
file_path
, but not infile_name
.Comment #11
Deciphered CreditAttribution: Deciphered commentedIt is a very tricky situation indeed, I entirely see the logic in what you're saying, but I don't like making decisions that break things for others.
The correct way of tackling this issue is to make that logic configurable by the user, allow them to make that decision. That can be done with the Rule Token module I provided, at least for those willing to delve into Rules, and for the others, I will keep thinking on exactly what I want to do.
However, the plan for sometime now has actually been to rebuild FFP with Rules in a new module, and then make FFP a sub-module of the new module that provided the same existing functionality of FFP, the same interface, but used the new module as the engine, so maybe the Rule Token module may just be the way it's going to be.... I can't yet say.
Comment #12
alex.skrypnyk@Deciphered
kaare's solution actually fixes the module to behave exactly how it is expected.
Pathauto should be cleaning up each tokenised value and then the rest of slashes should be used as path delimiters. I'm really struggling to understand how one would use this functionality otherwise (i.e. currently, if a token contains slash, the file is created in a folder).
Comment #13
alex.skrypnykBumping this to critical since files get misplaced.
Comment #14
alex.skrypnykProvided patch addresses the issue in another way by processing each token separately in token replacement callback.
Comment #15
Deciphered CreditAttribution: Deciphered commented@alex.designworks,
Please refer to https://drupal.org/comment/7965831#comment-7965831, implementing this path will break things for other users. Until a solution can be determined that allows both usecases to be catered for I can not implement a change.
Also, that patch does not appear to be rolled from HEAD and reintroduces code that has been removed and is unrelated to this issue.
And lastly, this issue is not critical as currently this is works as designed and a feature request to support an alternate usecase, and there are workarounds currently available.
Comment #16
alex.skrypnykThe initial patch https://drupal.org/files/1942720.patch works with replaced values instead of working with array of tokens=>values.
My patch uses callback that works with tokens=>values array directly. Although it does not have the logic to skip tokens that have ':join-path', it allows to add such logic to cover both cases _automatically_. This means that all occurrences of tokens with ':join-path' will continue having them replaced using native token module replacement mechanism; other tokens will have their values replaced using this module mechanism. In other words, the module will behave as expected, but will respect slashes in token values.
Also, the patch was rolled from 7.x. I'm not sure what other branch I should use.
Comment #17
rroblik CreditAttribution: rroblik commentedOk so about 6 month after last post ...
Is there any working solution to avoid slash owner token create subdirectories ?
eg : "My famous post 24 / 24 is all the day" (files/my-famous-post-24/24.jpg)
Thanks
Comment #18
BWPanda CreditAttribution: BWPanda commentedThis patch adds an option to remove slashes (/) from tokens. It then chooses when to processes string tokens based on that option.
Works for me. Review and see if this solves the above issues for everyone else.
Comment #19
kaare@BWPanda: Excellent idea to have an option for this. This will solve the dilemma between existing and expected behavior. Though this patch needs some work:
[node:title]
as part of file name token and creating an article named "Sub/dir" still puts the image in.../sub/dir-<filename>
.@Deciphered: Please respect the severity of this issue. Messing with subdirs in a file name is a bug and a potential security issue.
Comment #20
kaareComment #21
kaareThere are some questions still:
filefield_paths_process_string()
is doubled. This should probably be split up.Comment #22
kaareThis is a working edition with backwards compatibility. It works by removing slashes after pathauto has called it a day, thereby letting the site admin use pathauto to select what character '/' should be replaced with.
Comment #23
kaareThe last patch is a combination of @BWPanda's work in #18 and the patch in #6.
Comment #24
aytee CreditAttribution: aytee commentedThank you kaare! This solution in #23 worked for me and was exactly what I needed.
Comment #25
cmseasy CreditAttribution: cmseasy commented#23 worked for me, thanks.
The bulk rename proces is also faster!
Comment #26
UksusoFF CreditAttribution: UksusoFF commentedThx to kaare! Works like a charm.
Any way to hook cleanup rules for tokens?
Comment #27
UksusoFF CreditAttribution: UksusoFF commentedAnyone can update patch #23 for latest dev?
Comment #28
Deciphered CreditAttribution: Deciphered commented@UksusoFF,
No need for a re-roll, patch still applies to dev.
@kaare,
Thanks for the work, this meets all the requirements I had. I will be committing this at some point in the future (soon), but before I do so there does need to be some tests written. I will likely write them myself, but if you do feel so inclined, feel free to add them to the patch.
Comment #29
UksusoFF CreditAttribution: UksusoFF commented@Deciphered
I've unsuccessful tried apply this patch on previous dev version. On current version from 2015-Sep-08 it's work great.
Comment #30
Deciphered CreditAttribution: Deciphered commented@UksusoFF,
'patch -p1 < [patch]', works perfectly for me, but if it doesn't work for you then I'd suggest waiting till it's committed.
Comment #31
UksusoFF CreditAttribution: UksusoFF commented@Deciphered
Comment #32
Deciphered CreditAttribution: Deciphered commentedAh, misread. Good to hear.
Comment #33
Deciphered CreditAttribution: Deciphered commentedThanks for the patience and patches. Committed and pushed.