ibrod83 / nodejs-file-downloader

129 stars 23 forks source link

About skipExistingFileName and onBeforeSave #56

Closed YuJian920 closed 1 year ago

YuJian920 commented 1 year ago

I want to set a custom file name for the downloaded file and append its corresponding file extension. So I used onBeforeSave, but I found that the skipExistingFileName feature stopped working after using onBeforeSave. I can't skip existing files anymore. Is there a way to solve this?

let downloadHelper = new download({
  url,
  skipExistingFileName: true,
  onBeforeSave: (deducedName) => {
    const fileExt = extname(deducedName);
    if (fileExt) return `${item.id}-${item.desc}${fileExt}`;
    return deducedName;
  },
});
ibrod83 commented 1 year ago

i'll check the flow and let you know

ibrod83 commented 1 year ago

Currently there doesn't seem to be any way around it. There are too many options/hooks around the issue of the file name(a mistake, in retrospective). I might simplify this in the future

ibrod83 commented 1 year ago

Looking again at the code and after writing a test for this case, it seems that skipExistingFileName actually does work, being that the skipping conditional is invoked BEFORE onBeforeSave. Maybe in your case the problem is that you expect the skipping to be based on the filename you returned from onBeforeSave.

YuJian920 commented 1 year ago

😭Yes, what I expect is to skip existing files based on the filename returned by onBeforeSave.

ibrod83 commented 1 year ago

did u try using the "fileName" attribute instead of onBeforeSave? (as you can see, too many options..)

YuJian920 commented 1 year ago

If I use the fileName property, I can't dynamically change the file extension. But at least skipping files that already exist works, which is more important than customizing the extensions.