directory-upload icon indicating copy to clipboard operation
directory-upload copied to clipboard

easier way to determine if something is a directory?

Open marcoscaceres opened this issue 9 years ago • 7 comments

I see in the examples that you do:

filesAndDirs[i].getFilesAndDirectories === 'function'

This seems like a heavy handed way of filtering on "is a directory", no?

Basically, what I would want to do is:

ev.dataTransfer.getFilesAndDirectories()
  .then(fileOrDirs => items.map(
    fileOrDir => (fileOrDir.isDirectory) ? fileOrDir.getFilesAndDirectories() : Promise.resolve(fileOrDir)  
  ))
  .then(filePromises => Promise.all(filePromises))
  .then(uploadFiles)
  .catch(err);

Or (I haven't actually tried this, but you get the idea hopefully):

async function handleDrop(e){
  e.stopPropagation();
  e.preventDefault();
  // Not supported, so bail out?
  if (!('getFilesAndDirectories' in e.dataTransfer)){
      return;
  }
  let fileOrDirs = ev.dataTransfer.getFilesAndDirectories();
  let files = fileOrDirs.reduce((collection, next) => {
    let files = [];
    try{
      files = (item.isDirectory) ? await fileOrDir.getFilesAndDirectories() : [file];
    } catch (err){
      //do something useful, maybe 
    }
    collection.concat(files);
    return collection;
  });
 //finally
 upload(files);
}

marcoscaceres avatar Dec 01 '15 07:12 marcoscaceres

err... the reducer should be async too... but anyway.

marcoscaceres avatar Dec 01 '15 07:12 marcoscaceres

Argh, sorry... I should have continued scrolling past that first example. Just realized that Directory is an actual interface. So, filtering on those examples should really be done on item instanceof Directory, IMO (instead of the duck-type checks on a method).

marcoscaceres avatar Dec 01 '15 07:12 marcoscaceres

Although I am fond of idea of type-awareness all the way: item instanceof Directory. :+1: Would probably a good idea to take some inspiration for alternative approach of having a helper/utility function from node.js: https://nodejs.org/docs/v0.3.1/api/fs.html#fs.Stats.

ghost avatar Dec 01 '15 09:12 ghost

@marcoscaceres : is this issue still valid? Doesn't

item instanceof Directory

come out of the box? We don't really need fancy filtering, although I really like the use of the async function above.

As for a helper function, I think we get it with instanceof.

arunranga avatar Dec 28 '15 16:12 arunranga

I agree that we probably don't need an additional function to tell us if the object is a Directory since we already get that functionality from instanceof. I believe that there was some guidance early on that we shouldn't use instanceof because of multi-frame DOM concerns which I can't seem to recall now. Perhaps it isn't actually an issue and we can change the example from filesAndDirs[i].getFilesAndDirectories === 'function' to filesAndDirs[i] instanceof Directory?

aliams avatar Jan 20 '16 23:01 aliams

Those concerns, tho valid, are a little silly when we are discussing spec examples and common usage scenarios... of course, instance of window1.dir to iframe.contentWindow.Directory is going to return false - but that's not something people commonly do.

marcoscaceres avatar Feb 01 '16 10:02 marcoscaceres

Depending on where you got the object from instanceof Directory may not help you since Directory probably isn't a global object if this spec isn't implemented (i.e. you'll end up with a ReferenceError exception or something). For this example it would be fine though since we've already checked 'getFilesAndDirectories' in e.dataTransfer before entering this code, so it should be safe to assume Directory is a global.

Besides that it seems like if (typeof filesAndDirs[i].getFilesAndDirectories === 'function') is unnecessarily verbose anyway. Is there any reason this couldn't be if (filesAndDirs[i].getFilesAndDirectories)?

jwatt avatar Mar 23 '16 01:03 jwatt