Merge pull request #74 from juhchamp/master

Add `mimetype` and `privacy` option to `uploadFile` method of `Space`
This commit is contained in:
David Wakelin 2022-03-30 11:13:53 +01:00 committed by GitHub
commit f1adeb7c07
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 1 deletions

View file

@ -303,15 +303,19 @@ class Space
* *
* @param string $filepath The path to the file, including the filename. Relative and absolute paths are accepted. * @param string $filepath The path to the file, including the filename. Relative and absolute paths are accepted.
* @param string|null $filename The remote filename. If `null`, the local filename will be used. * @param string|null $filename The remote filename. If `null`, the local filename will be used.
* @param string|null $mimeType The file mime type to pass as ContentType for the file (e.g. 'image/jpeg').
* @param bool $private True for the file to be private, false to allow public access.
* *
* @return \SpacesAPI\File * @return \SpacesAPI\File
*/ */
public function uploadFile(string $filepath, ?string $filename = null): File public function uploadFile(string $filepath, ?string $filename = null, ?string $mimeType = null, bool $private = true): File
{ {
$this->s3->putObject([ $this->s3->putObject([
'Bucket' => $this->name, 'Bucket' => $this->name,
'Key' => ($filename) ?: basename($filepath), 'Key' => ($filename) ?: basename($filepath),
'SourceFile' => $filepath, 'SourceFile' => $filepath,
'ContentType' => $mimeType,
'ACL' => ($private) ? 'private' : 'public-read'
]); ]);
return new File($this, ($filename) ?: basename($filepath), [], false); return new File($this, ($filename) ?: basename($filepath), [], false);

View file

@ -86,6 +86,13 @@ class SpaceTest extends TestCase
$this->assertInstanceOf(File::class, $file); $this->assertInstanceOf(File::class, $file);
} }
public function testCanUploadFileWithMimeType()
{
$tmpFile = tempnam(sys_get_temp_dir(), 'spaces-test');
$file = self::$space->uploadFile($tmpFile, 'upload-test.txt', 'text/plain');
$this->assertInstanceOf(File::class, $file);
}
/** /**
* @depends testCanUploadText * @depends testCanUploadText
* @depends testCanUploadFile * @depends testCanUploadFile