WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Overriding files when using more than one attachment field (r 0.0.4) #23

@ifraixedes

Description

@ifraixedes

Hello,

My uses cases required me to have more than one mongoose-field in my schema and I did that, applying the plugin in this way:

 pageSchema.plugin(mongooseAttachments, {
    directory: 'images',
    storage: {
      providerName: 's3',
      options: {
        key: 'my aws key'
        secret: 'my aws secret'
        bucket: 'my aws bucket'
      }
    },
    properties: {
      design_background_image: {
        styles: {
          original: {
            //keep the original file
          },
          ipad_2_landscape: {
            resize: '1024x768>',
            blur: '3x3'
          },
          ipad_2_portrait: {
            resize: '768x1024>',
            blur: '3x3'
          }
      },
      design_banner_image: {
        styles: {
          original: {
            // keep the original file
          },
          ipad_2_landscape: {
            resize: '1024x768>'
          },
          ipad_2_portrait: {
            resize: '768x1024>'
          }
        }
      },
      avatar: {
        styles: {
          original: {
            // keep the original file
          }
        }
      }
    }
  });

The plugin works fine but I realized that if the two or more files to upload in two or more of the specified fields had the same extension then the files uploaded for each transformation were the same for all of them and it was one file between them.

After debugging to try to solve that issue and I could continue using this awesome module, I realized that the problem was (and is because I checked the code in the current file of the tag 0.0.4) in the line 170 of the file lib/attachments.js,

 var storageStylePath = '/' + options.directory + '/' + selfModel.id + '-' + styleName + ext;

and it may fix it quite easy only using the original file name which is figured in the line 151 rather than the document id.

However to avoid to remove the file extension of the file name in each call to forEach function I declared a new variable after the line 160 and I used that variable in the line 170, so the result was something like:

// Below the line 160
var fileNameNoExt = path.basename(attachmentInfo.name, fileExt);

........

// line 170 replaced by
var storageStylePath = '/' + options.directory + '/' + fileNameNoExt + '-' + styleName + ext;

I solved my use case however I am calling 'attach' with and stand-alone rather than using express uploads, and although I think that it would also work well, I cannot be 100% sure, because I haven't tested it in that way.

Could you update the release of npm to carry on using the module from npm rather than use like a local library in my development?

Thanks so much.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions