w3c/web-share

Should sharing zero size files be allowed?

Closed this issue · 10 comments

Should sharing of an opaque response be allowed?

try {
  const url = 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png';
  const blob = await fetch(url, {mode: 'no-cors'}).then(response => response.blob());
  const file = new File([blob], 'image.png');
  const data = {
    files: [file]
  };
  if (!navigator.canShare(data)) {
    throw new Error('Cannot share');
  }
  await navigator.share(data);
} catch (err) {
  console.error(err.name, err.message);
}

For this example, navigator.canShare() returns true, but the actual share operation then fails. Play with this example deployed.

Possibly related: #173

Why would response.blob() not throw here?

Gecko does not throw on .blob() here either, btw.

Yeah, I realized after posting that it likely won't fail, but the blob will be empty so you won't end up sharing anything.

Is just checking if the file is zero size sufficient? If yes, we could just add that check for each file (and return false).

Alternative: filter out all zero length files. If results in empty list of files, return false.

@tomayac, what do you think? Drop the empty files on the floor (and do nothing if they are all empty)? It seems like the most user friendly solution.

An empty file still has some info (the filename) so I wonder implicitly filtering it out may add some confusion.

Well, it depends. On MacOS's various handlers it's kinda confusing, because some apps just spin up with nothing to share (e.g., in Notes.app). The name doesn't seem to play a significant role.

Others show an empty preview of a file. In Messages.app, for example:

When normally you would see a preview:

What happens on Windows?

Looks like this when an "Share to" item is selected:

image

Ok, let's got with doing nothing here. Developer should decide and, if they so choose, filter out empty files for situations like @tomayac found. However, for certain applications (e.g., text), it could make perfect sense to share an empty text file with just a file name.