suggestedFilename in Playwright (docker) is not set correctly (default to "download")

75 views Asked by At

I'm trying to download a file with Playwright and use the download.suggestedFilename() to set the filename of the file.

This is my code :

  const downloadPromise = page.waitForEvent('download', {timeout:100000})
  await page.keyboard.down('Shift')
  await page.keyboard.press('KeyD')
  const download = await downloadPromise
  const tempPath = await download.path()
  console.log(' - tempPath : ' +tempPath)
  const suggestedFilename = await download.suggestedFilename()
  console.log(' - suggestedFilename : ' +suggestedFilename)

On my local machine (ubuntu, french) while running through npx tsx this is the output :

23/01/2024 22:04:20 ( 8.78) :  - tempPath : /tmp/playwright-artifacts-zUHKYv/ab49a46d-cdcb-46fb-bd5a-8695661713e3
23/01/2024 22:04:20 ( 8.78) :  - suggestedFilename : Congrès 24.jpg

And on my docker this is my output for the exact same URL :

23/01/2024 21:35:48 ( 13.90) :  - tempPath : /tmp/playwright-artifacts-qCYQZ2/713b56eb-6d85-474a-b40e-68fc28da088e
23/01/2024 21:35:48 ( 13.90) :  - suggestedFilename : download

I think this is something related with Content-Disposition header and the locale because all files without accent are working and all files with accent are named "download".

I tried to add these lined to my Dockerfile :

RUN apt-get install -y locales locales-all 
ENV LC_ALL fr_FR.UTF-8
ENV LANG fr_FR.UTF-8
ENV LANGUAGE fr_FR.UTF-8

but same problem.

This is the complete Dockerfile :

FROM mcr.microsoft.com/playwright

# Create app directory
WORKDIR /usr/src/app

RUN apt-get update && apt-get -y install cron
RUN apt-get install -y locales locales-all 
ENV LC_ALL fr_FR.UTF-8
ENV LANG fr_FR.UTF-8
ENV LANGUAGE fr_FR.UTF-8

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./

RUN npm ci
RUN npx playwright install --with-deps chrome

# Move app src
COPY src/. src/.
COPY tsconfig.json ./.
# Buld app
RUN npm run build

# Copy docker bashs cripts
COPY docker/job.sh .
RUN chmod +x ./job.sh

COPY docker/entrypoint.sh .
RUN chmod +x ./entrypoint.sh

# Create the log file to be able to run tail
RUN touch /var/log/cron.log
CMD [ "./entrypoint.sh" ]

Any idea ?

NB : to add some context, I'm working on a fork of https://github.com/CasperVerswijvelt/google-photos-backup

1

There are 1 answers

0
prigal On

I found a solution, I had to set locale and timezone in the context :

const browser = await chromium.launchPersistentContext(path.resolve(sessionDirectory), {
    headless,
    acceptDownloads: true,
    channel: 'chrome',
    args: ['--no-sandbox', '--disable-setuid-sandbox'],
    locale: 'fr-FR',
    timezoneId: 'Europe/Paris'
  })

Source : https://playwright.dev/docs/emulation#locale--timezone

Edit : just saw that Infern0 suggested the same thing. Too bad I didn't see your comment sooner :) Thank you.