1: <?php declare(strict_types=1);
2:
3: namespace Salient\Curler;
4:
5: use Salient\Http\HttpMultipartStreamPart;
6: use Salient\Utility\File;
7: use InvalidArgumentException;
8:
9: /**
10: * A file to upload to an HTTP endpoint
11: *
12: * @api
13: */
14: class CurlerFile extends HttpMultipartStreamPart
15: {
16: /**
17: * Creates a new CurlerFile object
18: *
19: * @param string|null $uploadFilename Default: `basename($filename)`
20: * @param string|null $mediaType Default: `mime_content_type($filename)`,
21: * `application/octet-stream` on failure.
22: */
23: public function __construct(
24: string $filename,
25: ?string $uploadFilename = null,
26: ?string $mediaType = null,
27: ?string $fallbackFilename = null,
28: ?string $name = null
29: ) {
30: if (!is_file($filename)) {
31: throw new InvalidArgumentException(sprintf(
32: 'File not found: %s',
33: $filename,
34: ));
35: }
36:
37: parent::__construct(
38: File::open($filename, 'r'),
39: $name,
40: $uploadFilename ?? basename($filename),
41: self::getFileMediaType($filename, $mediaType),
42: $fallbackFilename,
43: );
44: }
45: }
46: