Having a very bizarre problem on Drupal 8.4.4.

I have a custom module which accepts an image through a POST request and saves it as the submitting user's profile picture. This module has been in place for at least a year and has been working fine until recently.

This is the relevant section of code:

$syncfile = $_FILES ['file'];
if (isset ( $syncfile )) {
    if (strpos ( $syncfile ['type'], 'image' ) !== false) {
        $ext = explode ( "/", $syncfile ['type'] );
        $extension = $ext [count ( $ext ) - 1];
        $imgname = \Drupal::currentUser ()->id () . "_" . $pd->imagedate . "." . $extension;
        $img_dir = make_jpeg_target_size ( $syncfile ['tmp_name'], 3000 ); //this is a function that just resizes the image
        $img = getimagesize ( $img_dir );

        //save image to drupal's filesystem
        $path1 = "public://profile_images/";
        $path2 = \Drupal::service ( 'file_system' )->realpath ( file_default_scheme () . "://" ) . str_replace ( "/", DIRECTORY_SEPARATOR, "/profile_images/" );
        $file = File::create ( [ 
                'uid' => $user->id (),
                'filename' => $imgname,
                'uri' => $path1 . $imgname,
                'filemime' => $img ['mime'],
                'status' => FILE_STATUS_PERMANENT 
        ] );
        $file->save ();
        $fid = $file->id ();

        //delete existing profile image and replace with this one
        if (isset ( $user->field_profile_image->target_id )) {
            $f1 = File::load ( $user->field_profile_image->target_id );
            \Drupal::service ( 'file.usage' )->delete ( $f1, 'file', 'user', $user->id () );
        \Drupal::service ( 'file.usage' )->add ( $file, 'file', 'user', $user->id () );
        $user->field_profile_image->target_id = $fid;
        $user->save ();

I also have a rule in the conf.d file for my site to block external access to files of the apk, log, and pdf filetypes. This has also been in place for a while. The rule is not in .htaccess as that is part of drupal upgrades and would get overwritten.

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?<my domain> [NC]
RewriteRule \.(pdf|apk|log)$ - [NC,F,L]

Very recently, not sure exactly when, these two things have not been working together. When I disable the conf.d rule and make no other changes, and then test the module code, the new image shows up resized in the styles folder but the pdf/apk/log filetypes are not blocked. When I re-enable the code, the filetypes are blocked, but images sent to the module are not created in the styles folder. I am testing with different images and timestamps each time.

Neither Drupal nor Apache report any errors when this fails either, and Drupal verbose logging is enabled. The user's profile is making reference to the image that should be in the styles folder, so it's understood by the system that the resized image should exist, but the image is simply never put there and it draws a blank.

If I disable the apk/log/pdf block rule, the upload resize happens with no issue. This makes no sense as the block rule is not written to affect images. This has only stopped working in a recent upgrade, but not sure which one.

I tried this suggestion, but it did not fix the issue.

  • it's going to be a problem with the rule, can you add it to the question? And some sample URLs for your custom POST path and details of what that module actually does? Code snippets would be best. Also, derivatives are only generated when they're requested, not when an image is uploaded, have you taken that into account? – Clive Feb 9 '18 at 13:38
  • @Clive the code that accepts the image sets it as a user property and saves the user, so whenever the rule is inactive it works as expected. – saramm1 Feb 9 '18 at 13:47
  • Saving a user wouldn't generate a derivative either, only requesting the new image URL would (unless you've got something augmenting that like a D8 version of something like drupal.org/project/imageinfo_cache) – Clive Feb 9 '18 at 13:58
  • It appears I am not the only one experiencing this issue: drupal.org/forum/support/post-installation/2015-06-16/… – saramm1 Feb 12 '18 at 12:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.