Currently, the replaced file is forced to have the same extension as the original file. It is often needed to upload a new image file that has a different extension while retaining all the other field values on the file entity. Example: original.png, replacement.jpg

Comments

recrit’s picture

Status: Active » Needs review
StatusFileSize
new4.03 KB

The attached patch adds the following:
* Allows all mimetype extensions based on the file type settings.
* If the file name changes, then the file entity uri is updated and the original file in the file system is deleted (ie no the file entity).

Status: Needs review » Needs work

The last submitted patch, 1: file_entity-replace-allow-type-extensions-2271229-1.patch, failed testing.

recrit’s picture

update patch to copy and rename if new file has a different name than the original. This will help avoid collisions with other existing files.

recrit’s picture

Status: Needs work » Needs review
recrit’s picture

StatusFileSize
new1.13 KB

adding interdiff

recrit’s picture

rolled against the latest 7.x-2.x 5e9c095

mglaman’s picture

Status: Needs review » Reviewed & tested by the community

Awesome, this will make some clients happy. Especially useful if you uploaded a video as JPEG and then realized "oh no, that should be transparent PNG" or something crazy like that.

fabianx’s picture

Nice!

RTBC + 1

+++ b/file_entity.pages.inc
@@ -743,14 +758,28 @@ function file_entity_edit_validate($form, &$form_state) {
+        // Update file enity uri.

nit - typo

catch’s picture

Also agreed on RTBC - I've tested this manually and via automated behat tests for a client.

recrit’s picture

fixed misspelling

stuchl4n3k’s picture

Would somebody please explain, why it was designed to maintained extensions in the first place?

fabianx’s picture

Because it was designed to keep the same filename, so when you had originally uploaded a JPG file called test.jpg and then uploaded a GIF, you would end up with test.jpg with GIF content :-D, which is quite bad.

The reason was / is that images can be e.g. inserted into text areas and the links to them are persistent.

  • aaron committed 9a13484 on 7.x-2.x
    Issue #2271229 by recrit: Allow file replacement extensions for the same...
aaron’s picture

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

Status: Fixed » Needs work

Re-opening this for some follow-ups and missing test coverage for the new API function as well as replacement functionality.

  1. +++ b/file_entity.pages.inc
    @@ -791,6 +820,15 @@ function file_entity_edit_submit($form, &$form_state) {
    +    file_unmanaged_delete($orphaned_uri);
    

    Should this logic actually be in file_entity_file_update() checking to see if $file->original->uri != $file->uri?

  2. +++ b/file_entity.pages.inc
    @@ -791,6 +820,15 @@ function file_entity_edit_submit($form, &$form_state) {
    +    drupal_set_message(t('The replaced @type @orphaned has been deleted.', $args));
    

    A message isn't really desired here, we should remove this.

bneil’s picture

Issue tags: +Needs tests
dave reid’s picture

Priority: Normal » Major
Issue tags: +7.x-2.0 release blocker
haleagar’s picture

Just a note: this causes a drastic change in behaviour compared in file_entity 7.x-2.0-beta1 and file_entity 7.x-2.0-beta2 vs file_entity 7.x-2.0-alpha.

In file_entity 7.x-2.0-alpha and previous versions if you use the file replace option the original file name (in the file system) was retained and all hard links to that file remain valid.
After this change the file name (in the file system) is changed and any links to the file such as use in a wysiwyg field are broken.

I realize that either behaviour could be argued as correct, but in my opinion the previous behaviour is a more desirable and expected behaviour than the new.
I would say it is *much less* often needed to upload a new image file that has a different extension, than it is to replace a file with one of the same type.

I would suggest that it would be desirable to modify this function such that the file name is only changed when needed by a change in the file extension, or possibly only when expressly requested by the user by a option on the edit form.

haleagar’s picture

Here is a simple patch on current Oct 3 2015 dev (7.x-2.0-beta2+13-dev) that does what I suggested above to maintain the file name if the new file is of the same type.

gmclelland’s picture

Status: Needs work » Needs review

Patch attached. NR

chaseontheweb’s picture

Status: Needs review » Needs work

I've opened #2820380: File replace no longer preserves original filename to address the issue @haleagar brought up.

Setting this issue back to NW for Dave's comments in #15.

joseph.olstad’s picture

Assigned: Unassigned » joseph.olstad

joseph.olstad’s picture

Status: Needs work » Fixed

see related issue
making this change, 2 years long enough without any objections.
#2820380: File replace no longer preserves original filename

Status: Fixed » Closed (fixed)

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