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

marcingy’s picture

Version: 7.x-3.1 » 7.x-3.x-dev

This 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.

wedge’s picture

Status: Active » Needs review
StatusFileSize
new1.19 KB

Like this?

marcingy’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to me.

kylebrowning’s picture

Status: Reviewed & tested by the community » Fixed
wedge’s picture

Status: Fixed » Needs work

Maybe 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.

kylebrowning’s picture

kylebrowning’s picture

Category: bug » support
Priority: Normal » Major
Status: Needs work » Active

Ive 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.

kylebrowning’s picture

Category: support » bug

whoops

Jorrit’s picture

The "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.

wedge’s picture

StatusFileSize
new1.12 KB

Thanks for the hints Jorrit. Maybe something like this patch makes sense then?

Jorrit’s picture

I think it should work. Personally, I also need these changes for create_raw.

kylebrowning’s picture

Can you re-roll this patch it doesnt apply to latest dev.

wedge’s picture

StatusFileSize
new805 bytes

Re-rolled

kylebrowning’s picture

Status: Active » Needs review
jhr’s picture

As 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.
// Load the file via file.fid.
$file = file_load($form_state['values']['my_file_field']);
// Change status to permanent.
$file->status = FILE_STATUS_PERMANENT;
// Save.
file_save($file);
// Record that the module (in this example, user module) is using the file.
file_usage_add($file, 'user', 'user', $account->uid);

(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. ?

kylebrowning’s picture

File id's cannot be changed. they must be re-uploaded to get a new id.

wedge’s picture

StatusFileSize
new809 bytes

Patch in #13 was wrong. New version attached.

ygerasimov’s picture

StatusFileSize
new4.34 KB

Here is patch from #17 with added test to show how to upload file and "attach" it to the node.

wedge’s picture

Status: Needs review » Reviewed & tested by the community

@ygerasimov. Works as expected and tests passes here to. Thanks for bringing this home.

kylebrowning’s picture

Go ahead on this one. Great work gentlemen(maybe some ladies?)

ygerasimov’s picture

Status: Reviewed & tested by the community » Fixed

Committed. Thank you! e5d71e9

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.