roopakv / google-photos

Library to make hitting the Google Photos api easy
MIT License
56 stars 22 forks source link

Request change to uploadMultiple #63

Open rayzorben opened 1 month ago

rayzorben commented 1 month ago

I have a bunch of files across a bunch of directories, so I modified it to support that, would love to see official support

  async uploadMultiple2(
    albumId,
    files, // { name: 'filename', path: 'filepath', description: 'description' }
    requestDelay = 10000,
    requestTimeout = 10000
  ) {
    const url = `${constants.BASE_PATH}/:batchCreate`;
    const batchedFiles = chunk(files, 50);
    // eslint-disable-next-line
    for (const batch of batchedFiles) {
      // eslint-disable-next-line
      const newMediaItems = await Promise.all(
        batch.map(async (file) => {
          const token = await this.transport.upload(file.name, file.path, requestTimeout);

          return {
            description: file.description || '',
            simpleMediaItem: {
              fileName: file.name,
              uploadToken: token,
            },
          };
        })
      );
      this.transport.post(url, {
        albumId: albumId || '',
        newMediaItems,
      });
      // google upload token generation seems to cap at ~500 requests per minute, this is a configurable workaround
      // eslint-disable-next-line
      await new Promise((resolve) => setTimeout(resolve, requestDelay));
    }
  }
roopakv commented 1 month ago

hi @rayzorben do you want to open a PR? happy to take a look and get it published

roopakv commented 1 month ago

out of curiosity what happens if you pass in an empty directory path say '' and put the whole path in filepath? i feel like that would solve your problem without needing a new method