Files
flipper/desktop/flipper-ui-core/src/utils/screenshot.tsx
Andres Suarez 79023ee190 Update copyright headers from Facebook to Meta
Reviewed By: bhamodi

Differential Revision: D33331422

fbshipit-source-id: 016e8dcc0c0c7f1fc353a348b54fda0d5e2ddc01
2021-12-27 14:31:45 -08:00

46 lines
1.5 KiB
TypeScript

/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
import BaseDevice from '../devices/BaseDevice';
import {reportPlatformFailures} from 'flipper-common';
import {getRenderHostInstance} from '../RenderHost';
import {getFlipperLib, path} from 'flipper-plugin';
export function getCaptureLocation() {
return (
getRenderHostInstance().serverConfig.processConfig.screenCapturePath ||
getRenderHostInstance().serverConfig.paths.desktopPath
);
}
// TODO: refactor so this doesn't need to be exported
export function getFileName(extension: 'png' | 'mp4'): string {
// Windows does not like `:` in its filenames. Yes, I know ...
return `screencap-${new Date().toISOString().replace(/:/g, '')}.${extension}`;
}
export async function capture(device: BaseDevice): Promise<string> {
if (!device.connected.get()) {
console.log('Skipping screenshot for disconnected device');
return '';
}
const pngPath = path.join(getCaptureLocation(), getFileName('png'));
return reportPlatformFailures(
// TODO: there is no reason to read the screenshot first, grab it over the websocket, than send it back
// again to write in a file, probably easier to change screenshot api to `device.screenshot(): path`
device
.screenshot()
.then((buffer) =>
getFlipperLib().remoteServerContext.fs.writeFileBinary(pngPath, buffer),
)
.then(() => pngPath),
'captureScreenshot',
);
}