diff --git a/desktop/app/src/chrome/ScreenCaptureButtons.tsx b/desktop/app/src/chrome/ScreenCaptureButtons.tsx index 76b61076b..6c169dbf3 100644 --- a/desktop/app/src/chrome/ScreenCaptureButtons.tsx +++ b/desktop/app/src/chrome/ScreenCaptureButtons.tsx @@ -10,6 +10,7 @@ import {Button as AntButton, message} from 'antd'; import React, {useState, useEffect, useCallback} from 'react'; import path from 'path'; +import fs from 'fs-extra'; import open from 'open'; import {capture, CAPTURE_LOCATION, getFileName} from '../utils/screenshot'; import {CameraOutlined, VideoCameraOutlined} from '@ant-design/icons'; @@ -20,10 +21,27 @@ async function openFile(path: string | null) { return; } + let fileStat; + try { + fileStat = await fs.stat(path); + } catch (err) { + message.error(`Couldn't open captured file: ${path}: ${err}`); + return; + } + + // Rather randomly chosen. Some FSs still reserve 8 bytes for empty files. + // If this doesn't reliably catch "corrupt" files, you might want to increase this. + if (fileStat.size <= 8) { + message.error( + 'Screencap file retrieved from device appears to be corrupt. Your device may not support screen recording. Sometimes restarting your device can help.', + ); + return; + } + try { await open(path); } catch (e) { - console.error(`Opening ${path} failed with error ${e}.`); + console.warn(`Opening ${path} failed with error ${e}.`); } }