I use the Services to upload photo to my gallery. However, the photo then cannot be deleted because it always say that it is being used by another module, and that module is the Services.
The code in the file_resource.inc:
// Required to be able to reference this file.
file_usage_add($file, 'services', 'files', $file->fid);
I think this usage is not relevant, because the Services just upload file for others to use. The file should not be protected for Services. Adding this usage will prevent the user from deleting file.
Comments
Comment #1
marcingy commentedThis pretty much makes sense to remove as file_field_insert and file_field_update are going to add a reference I'll try and roll a patch later today or tomorrow.
Comment #2
wedge commentedLike this?
Comment #3
marcingy commentedLooks good to me.
Comment #4
kylebrowning commentedComment #5
wedge commentedMaybe this is needed after all. :(
When I try to create a node now with a file attachment that was just uploaded I get:
406 "The file used in the Image attachments field may not be referenced."
This is a form_error from file.module:556 file_managed_file_validate() method.
What is the correct way to upload a file and create a node with this file attached? If the behavior is changed with this patch, maybe this has to be documented somewhere.
Comment #6
kylebrowning commented#1484992: Attach file to node targeted_action
Comment #7
kylebrowning commentedIve reverted this commit Uploading files works but it needs some love. Let use this ticket as a task to make sure its working.
Its hard to call this a bug, but it basically is and I want to resolve it asap. Basically file saving in 7.x is a bit wonky and we need to make sure its perfect before 3.2 release.
Comment #8
kylebrowning commentedwhoops
Comment #9
Jorrit commentedThe "may not be referenced" error only occurs when the file is permanent. Normally, the following happens: using the node form a file is uploaded via AJAX. It is stored in the file_managed table as temporary and not yet registered in file_usage. The fid is embedded in the form and when the entire node add/edit form is submitted, the file is made permanent and linked to the node. If the file is not linked for some reason, it will be cleaned up after six hours.
This issue is related to #1563130: Resource file:create_raw should set file status to permanent. Perhaps there should be two methods. One that creates a temporary file and does not register it in file_usage, which can be used prior to creating or editing a node. Another method would create a permanent file and register it in file_usage. This method can be used to upload a file without using it for a node or other entity.
Comment #10
wedge commentedThanks for the hints Jorrit. Maybe something like this patch makes sense then?
Comment #11
Jorrit commentedI think it should work. Personally, I also need these changes for create_raw.
Comment #12
kylebrowning commentedCan you re-roll this patch it doesnt apply to latest dev.
Comment #13
wedge commentedRe-rolled
Comment #14
kylebrowning commentedComment #15
jhr commentedAs mentioned in #9
Note: New files are uploaded with a status of 0 and are treated as temporary files which are removed after 6 hours via cron. Your module is responsible for changing the $file objects status to FILE_STATUS_PERMANENT and saving the new status to the database. Something like the following within your submit handler should do the trick.
(Without the call to file_usage_add, file_managed_file_validate produces an error upon saving the form, saying that the uploaded file may not be referenced.)
Quoted from FAPI's field #type => #managed_file It creates the file form field with ajax upload/remove buttons for the file field.
http://api.drupal.org/api/drupal/developer!topics!forms_api_reference.ht...
The question becomes, Who's file is it anyway?
My {file_usage} table only has files with module: file , and type: node.
I don't have file fields attached to any other node types, but I imagine these comments on d.o would be another 'type'.
{file_usage} descriptions of the fields
module: The name of the module that is using the file.
type: The name of the object type in which the file is used.
id: The primary key of the object using the file.
Can id be changed later w/ file resource -> update?
Maybe if the module was set to file, and the type set to services... it could still be deleted. ?
Comment #16
kylebrowning commentedFile id's cannot be changed. they must be re-uploaded to get a new id.
Comment #17
wedge commentedPatch in #13 was wrong. New version attached.
Comment #18
ygerasimov commentedHere is patch from #17 with added test to show how to upload file and "attach" it to the node.
Comment #19
wedge commented@ygerasimov. Works as expected and tests passes here to. Thanks for bringing this home.
Comment #20
kylebrowning commentedGo ahead on this one. Great work gentlemen(maybe some ladies?)
Comment #21
ygerasimov commentedCommitted. Thank you! e5d71e9