mirror of
https://github.com/microsoft/playwright.git
synced 2025-06-26 21:40:17 +00:00
fix: compute file field mime type on the server (#10394)
This commit is contained in:
parent
bd93fc499f
commit
0ca10da166
@ -16,7 +16,6 @@
|
|||||||
|
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import * as mime from 'mime';
|
|
||||||
import * as util from 'util';
|
import * as util from 'util';
|
||||||
import { Serializable } from '../../types/structs';
|
import { Serializable } from '../../types/structs';
|
||||||
import * as api from '../../types/types';
|
import * as api from '../../types/types';
|
||||||
@ -283,11 +282,7 @@ export class APIResponse implements api.APIResponse {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type ServerFilePayload = {
|
type ServerFilePayload = NonNullable<channels.FormField['file']>;
|
||||||
name: string,
|
|
||||||
mimeType: string,
|
|
||||||
buffer: string,
|
|
||||||
};
|
|
||||||
|
|
||||||
function filePayloadToJson(payload: FilePayload): ServerFilePayload {
|
function filePayloadToJson(payload: FilePayload): ServerFilePayload {
|
||||||
return {
|
return {
|
||||||
@ -307,7 +302,6 @@ async function readStreamToJson(stream: fs.ReadStream): Promise<ServerFilePayloa
|
|||||||
const streamPath: string = Buffer.isBuffer(stream.path) ? stream.path.toString('utf8') : stream.path;
|
const streamPath: string = Buffer.isBuffer(stream.path) ? stream.path.toString('utf8') : stream.path;
|
||||||
return {
|
return {
|
||||||
name: path.basename(streamPath),
|
name: path.basename(streamPath),
|
||||||
mimeType: mime.getType(streamPath) || 'application/octet-stream',
|
|
||||||
buffer: buffer.toString('base64'),
|
buffer: buffer.toString('base64'),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@ -253,7 +253,7 @@ export type FormField = {
|
|||||||
value?: string,
|
value?: string,
|
||||||
file?: {
|
file?: {
|
||||||
name: string,
|
name: string,
|
||||||
mimeType: string,
|
mimeType?: string,
|
||||||
buffer: Binary,
|
buffer: Binary,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
@ -224,7 +224,7 @@ FormField:
|
|||||||
type: object?
|
type: object?
|
||||||
properties:
|
properties:
|
||||||
name: string
|
name: string
|
||||||
mimeType: string
|
mimeType: string?
|
||||||
buffer: binary
|
buffer: binary
|
||||||
|
|
||||||
APIRequestContext:
|
APIRequestContext:
|
||||||
|
|||||||
@ -153,7 +153,7 @@ export function createScheme(tChannel: (name: string) => Validator): Scheme {
|
|||||||
value: tOptional(tString),
|
value: tOptional(tString),
|
||||||
file: tOptional(tObject({
|
file: tOptional(tObject({
|
||||||
name: tString,
|
name: tString,
|
||||||
mimeType: tString,
|
mimeType: tOptional(tString),
|
||||||
buffer: tBinary,
|
buffer: tBinary,
|
||||||
})),
|
})),
|
||||||
});
|
});
|
||||||
|
|||||||
@ -14,7 +14,8 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import * as types from './types';
|
import mime from 'mime';
|
||||||
|
import * as channels from '../protocol/channels';
|
||||||
|
|
||||||
export class MultipartFormData {
|
export class MultipartFormData {
|
||||||
private readonly _boundary: string;
|
private readonly _boundary: string;
|
||||||
@ -35,10 +36,10 @@ export class MultipartFormData {
|
|||||||
this._finishMultiPartField();
|
this._finishMultiPartField();
|
||||||
}
|
}
|
||||||
|
|
||||||
addFileField(name: string, value: types.FilePayload) {
|
addFileField(name: string, value: NonNullable<channels.FormField['file']>) {
|
||||||
this._beginMultiPartHeader(name);
|
this._beginMultiPartHeader(name);
|
||||||
this._chunks.push(Buffer.from(`; filename="${value.name}"`));
|
this._chunks.push(Buffer.from(`; filename="${value.name}"`));
|
||||||
this._chunks.push(Buffer.from(`\r\ncontent-type: ${value.mimeType || 'application/octet-stream'}`));
|
this._chunks.push(Buffer.from(`\r\ncontent-type: ${value.mimeType || mime.getType(value.name) || 'application/octet-stream'}`));
|
||||||
this._finishMultiPartHeader();
|
this._finishMultiPartHeader();
|
||||||
this._chunks.push(Buffer.from(value.buffer, 'base64'));
|
this._chunks.push(Buffer.from(value.buffer, 'base64'));
|
||||||
this._finishMultiPartField();
|
this._finishMultiPartField();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user