async function resizeImage(storage, imageResizer, entry, src, previewSizes, sizeToImagePreviewSuffix) {
  const firstMissingIndex = previewSizes.findIndex(size => !storage.hasFile(entry, sizeToImagePreviewSuffix(size)))
  if (firstMissingIndex < 0) {
    return []
  }

  let createdPreviewSizes = []
  /** @type {import('@home-gallery/types').TLocalStorageFile} */
  let newFiles = []
  /** @type {import('@home-gallery/types').TLocalStorageFile} */
  let existingFiles = []

  const missingPreviewSizes = previewSizes.slice(firstMissingIndex)
  if (firstMissingIndex > 0) {
    const lastSize = previewSizes[firstMissingIndex - 1]
    const localFile = await storage.createLocalFile(entry, sizeToImagePreviewSuffix(lastSize))
    src = localFile.file
    existingFiles.push(localFile)
  }

  let index = 0;
  const next = async () => {
    if (index === missingPreviewSizes.length) {
      return
    }

    const size = missingPreviewSizes[index++];
    const suffix = sizeToImagePreviewSuffix(size);

    if (storage.hasFile(entry, suffix)) {
      const localFile = await storage.createLocalFile(entry, suffix)
      existingFiles.push(localFile)
      src = localFile.file
      return next();
    }

    const newFile = await storage.createLocalFile(entry, suffix)
    newFiles.push(newFile)
    return imageResizer(src, newFile.file, size)
      .then(() => {
        createdPreviewSizes.push(size)
        src = newFile.file
        return next()
      })
  }

  return next()
    .then(() => {
      return createdPreviewSizes
    })
    .finally(async () => {
      // Cleanup local storage files
      await Promise.all(newFiles.map(file => file.commit()))
      await Promise.all(existingFiles.map(file => file.release()))
      newFiles = []
      existingFiles = []
    })
}
@param {import('@home-gallery/types').TStorage} storage
@param {import('./image-resizer.js').ImageResizer} imageResizer
@param {import('@home-gallery/types').TExtractorEntry} entry
@param {string} src
@param {number[]} previewSizes
@param {(size: number) => string} sizeToImagePreviewSuffix