export async function uploadScreenshotToGCS()

in packages/synthetics-sdk-broken-links/src/storage_func.ts [149:192]


export async function uploadScreenshotToGCS(
  page: Page,
  storageParams: StorageParameters,
  options: BrokenLinksResultV1_BrokenLinkCheckerOptions
): Promise<ApiScreenshotOutput> {
  const screenshot_output: ApiScreenshotOutput = {
    screenshot_file: '',
    screenshot_error: {} as BaseError,
  };
  try {
    // Early exit if storage is not properly configured
    if (!storageParams.storageClient || !storageParams.bucket) {
      return screenshot_output;
    }

    const screenshot: Buffer = await page.screenshot({
      fullPage: true,
      encoding: 'binary',
    });
    const filename = 'screenshot_' + storageParams.screenshotNumber + '.png';

    const writeDestination = path.join(
      getStoragePathToExecution(storageParams, options),
      filename
    );

    // Upload to GCS
    await storageParams.bucket.file(writeDestination).save(screenshot, {
      contentType: 'image/png',
    });

    storageParams.screenshotNumber += 1;
    screenshot_output.screenshot_file = filename;
  } catch (err) {
    console.error('ScreenshotFileUploadError', err);

    screenshot_output.screenshot_error = {
      error_type: 'ScreenshotFileUploadError',
      error_message: `Failed to take and/or upload screenshot for ${await page.url()}. Please reference server logs for further information.`,
    };
  }

  return screenshot_output;
}