fileUploadUtils.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.prepareFilesForUpload = prepareFilesForUpload;
  6. var _fs = _interopRequireDefault(require("fs"));
  7. var _path = _interopRequireDefault(require("path"));
  8. var _utils = require("../utils");
  9. var _utilsBundle = require("../utilsBundle");
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11. /**
  12. * Copyright (c) Microsoft Corporation.
  13. *
  14. * Licensed under the Apache License, Version 2.0 (the "License");
  15. * you may not use this file except in compliance with the License.
  16. * You may obtain a copy of the License at
  17. *
  18. * http://www.apache.org/licenses/LICENSE-2.0
  19. *
  20. * Unless required by applicable law or agreed to in writing, software
  21. * distributed under the License is distributed on an "AS IS" BASIS,
  22. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. * See the License for the specific language governing permissions and
  24. * limitations under the License.
  25. */
  26. async function filesExceedUploadLimit(files) {
  27. const sizes = await Promise.all(files.map(async file => (await _fs.default.promises.stat(file)).size));
  28. return sizes.reduce((total, size) => total + size, 0) >= _utils.fileUploadSizeLimit;
  29. }
  30. async function prepareFilesForUpload(frame, params) {
  31. var _fileBuffers;
  32. const {
  33. payloads,
  34. streams
  35. } = params;
  36. let {
  37. localPaths
  38. } = params;
  39. if ([payloads, localPaths, streams].filter(Boolean).length !== 1) throw new Error('Exactly one of payloads, localPaths and streams must be provided');
  40. if (streams) localPaths = streams.map(c => c.path());
  41. if (localPaths) {
  42. for (const p of localPaths) (0, _utils.assert)(_path.default.isAbsolute(p) && _path.default.resolve(p) === p, 'Paths provided to localPaths must be absolute and fully resolved.');
  43. }
  44. let fileBuffers = payloads;
  45. if (!frame._page._browserContext._browser._isCollocatedWithServer) {
  46. // If the browser is on a different machine read files into buffers.
  47. if (localPaths) {
  48. if (await filesExceedUploadLimit(localPaths)) throw new Error('Cannot transfer files larger than 50Mb to a browser not co-located with the server');
  49. fileBuffers = await Promise.all(localPaths.map(async item => {
  50. return {
  51. name: _path.default.basename(item),
  52. buffer: await _fs.default.promises.readFile(item),
  53. lastModifiedMs: (await _fs.default.promises.stat(item)).mtimeMs
  54. };
  55. }));
  56. localPaths = undefined;
  57. }
  58. }
  59. const filePayloads = (_fileBuffers = fileBuffers) === null || _fileBuffers === void 0 ? void 0 : _fileBuffers.map(payload => ({
  60. name: payload.name,
  61. mimeType: payload.mimeType || _utilsBundle.mime.getType(payload.name) || 'application/octet-stream',
  62. buffer: payload.buffer.toString('base64'),
  63. lastModifiedMs: payload.lastModifiedMs
  64. }));
  65. return {
  66. localPaths,
  67. filePayloads
  68. };
  69. }