When saving a new file through services file.save, if the file gets renamed to filename_0.ext because filename.ext already exists, the files table ends up with in wrong value.
The version I have now are drupal 6.19 and services 6.x-2.4.
I'm quite sure that this used to work.
After looking into the code, I made the following changes in the file_service.inc
// Write the file
$newfilename = $file->filename;
if (!$newfilename = file_save_data(base64_decode($file->file), $file->filepath .'/'. $file->filename)) {
return services_error("Could not write file to destination");
}
//The filepath that ends up in the node must contain the filename
$file->filepath .= '/'. $newfilename;
I don't know if it is written in the best possible way, but it works for me.
If anyone has more info on that I would like to hear.
Jean-Paul
Comments
Comment #1
miraclegr commentedSorry the above code has a bug too. The correct one is
Comment #2
brad.bulger commentedthe problem is that the call to file_check_path() is changing $file->filepath to just the directory name, and losing the modified filename. this patch rearranges things somewhat, to be more like file_save_upload(). it's been working for me using it with the Deploy module. it's only a tentative fix, as there is a pending patch to file_save_upload() that does away with some steps, from #30520: file_save_upload() not notifying user when PHP upload limit is exceeded.
Comment #3
marcingy commentedThis needs to be fixed in 7.x first and then backported.
Comment #4
ygerasimov commented@marcingy file resource is not good yet on 6.x-3.x and there is no tests that cover it. 7.x-3.x file_resource is much better. I will take another round to make file resource work on 6.x-3.x.
Moving this issue to 6.x-3.x. Any help will be very appreciated
Comment #5
ygerasimov commentedHere is patch that changes file_resource plus adds tests for 6.x-3.x. Please remember to apply patch from #180970: file_check_directory fails to make directories if the parent doesn't already exist in order tests pass as simpletest cannot create files structure without that patch.
Regarding this particular issue (rename file if file with same name exists) there is check in testResourceFileCreate().
Comment #6
ygerasimov commentedHere is patch that does not require changes in d6 core.
Comment #7
kylebrowning commentedTheres a watchdog statement in there, but other than that it looks great. Ill RTBC it when thats removed.
Comment #8
ygerasimov commentedOh... Attached patch without watchdog.
Comment #9
kylebrowning commented