Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release 18.0.0 #349

Merged
merged 9 commits into from
Nov 30, 2022
8 changes: 5 additions & 3 deletions src/qtism/common/datatypes/files/FileManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2014-2020 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
* Copyright (c) 2014-2022 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
*
* @author Jérôme Bogaerts <jerome@taotesting.com>
* @license GPLv2
Expand Down Expand Up @@ -57,18 +57,20 @@ public function createFromFile($path, $mimeType, $filename = '');
* @param string $data A binary string representing the data.
* @param string $mimeType The MIME type of the resulting File object.
* @param string $filename The filename of the resulting File object.
* @param string|null $path A path for file provided externally of the resulting File object
* @return QtiFile
* @throws FileManagerException
*/
public function createFromData($data, $mimeType, $filename = '');
public function createFromData($data, $mimeType, $filename = '', $path = null);

/**
* Retrieve a previously created instance by $identifier.
*
* @param string $identifier
* @param string|null $filename
* @throws FileManagerException
*/
public function retrieve($identifier);
public function retrieve($identifier, $filename = null);

/**
* Delete a given QtiFile from its storage.
Expand Down
12 changes: 7 additions & 5 deletions src/qtism/common/datatypes/files/FileSystemFileManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2014-2020 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
* Copyright (c) 2014-2022 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
*
* @author Jérôme Bogaerts <jerome@taotesting.com>
* @license GPLv2
Expand Down Expand Up @@ -93,12 +93,13 @@ public function createFromFile($path, $mimeType, $filename = '')
* @param string $data The binary data of the FileSystemFile object to be created.
* @param string $mimeType A mime-type.
* @param string $filename A file name e.g. "myfile.txt".
* @param string|null $path A path for file provided externally
* @return FileSystemFile
* @throws FileManagerException
*/
public function createFromData($data, $mimeType, $filename = '')
public function createFromData($data, $mimeType, $filename = '', $path = null)
{
$destination = $this->buildDestination();
$destination = $path ?: $this->buildDestination();

try {
return FileSystemFile::createFromData($data, $destination, $mimeType, $filename);
Expand All @@ -111,11 +112,12 @@ public function createFromData($data, $mimeType, $filename = '')
/**
* Retrieve a FileSystemFile object from its unique identifier.
*
* @param string identifier
* @param string $identifier
* @param string|null $filename
* @return FileSystemFile
* @throws FileManagerException
*/
public function retrieve($identifier)
public function retrieve($identifier, $filename = null)
{
try {
return FileSystemFile::retrieveFile($identifier);
Expand Down
1 change: 1 addition & 0 deletions src/qtism/runtime/pci/json/Marshaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ protected function marshallFile(QtiFile $file)
'file' => [
'mime' => $file->getMimeType(),
'data' => base64_encode($file->getData()),
'path' => $file->getIdentifier()
],
],
];
Expand Down
5 changes: 3 additions & 2 deletions src/qtism/runtime/pci/json/Unmarshaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2014-2020 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
* Copyright (c) 2014-2022 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
*
* @author Jérôme Bogaerts <jerome@taotesting.com>
* @license GPLv2
Expand Down Expand Up @@ -372,7 +372,8 @@ protected function unmarshallFile(array $unit)
return $this->getFileManager()->createFromData(
base64_decode($fileArray['data']),
$fileArray['mime'],
$fileArray['name'] ?? ''
$fileArray['name'] ?? '',
$fileArray['path'] ?? null
);
}

Expand Down
8 changes: 6 additions & 2 deletions src/qtism/runtime/storage/binary/QtiBinaryStreamAccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ class QtiBinaryStreamAccess extends BinaryStreamAccess
self::ENCODED_TEMPLATE_DECLARATION => 'templateDeclaration',
];

private const FILE_IDENTIFIER_ENCODING_MASK = '%s : %s';

/** @var FileManager */
private $fileManager;

Expand Down Expand Up @@ -1156,7 +1158,7 @@ public function writeFile(QtiFile $file)
{
$toPersist = $file instanceof FileHash
? json_encode($file)
: $file->getIdentifier();
: sprintf(self::FILE_IDENTIFIER_ENCODING_MASK, $file->getIdentifier(), $file->getFilename());

try {
$this->writeString($toPersist);
Expand Down Expand Up @@ -1187,7 +1189,9 @@ public function readFile()
return FileHash::createFromArray($decoded);
}

return $this->getFileManager()->retrieve($id);
[$id, $filename] = sscanf($id, self::FILE_IDENTIFIER_ENCODING_MASK);

return $this->getFileManager()->retrieve($id, $filename);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function testRetrieve()
{
$manager = new FileSystemFileManager();
$mFile = $manager->createFromFile(self::samplesDir() . 'datatypes/file/raw/text.txt', 'text/plain', 'newname.txt');
$mFile = $manager->retrieve($mFile->getIdentifier());
$mFile = $manager->retrieve($mFile->getIdentifier(), $mFile->getFilename());
$this::assertEquals('text/plain', $mFile->getMimeType());
$this::assertEquals('newname.txt', $mFile->getFilename());
$this::assertEquals('I contain some text...', $mFile->getData());
Expand Down
4 changes: 2 additions & 2 deletions test/qtismtest/runtime/pci/json/JsonMarshallerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,10 @@ public function marshallComplexProvider()
$returnValue[] = [new QtiDuration('P3DT4H'), json_encode(['base' => ['duration' => 'P3DT4H']])];

$file = new FileSystemFile($samples . 'datatypes/file/text-plain_text_data.txt');
$returnValue[] = [$file, json_encode(['base' => ['file' => ['mime' => $file->getMimeType(), 'data' => base64_encode($file->getData()), 'name' => 'text.txt']]])];
$returnValue[] = [$file, json_encode(['base' => ['file' => ['mime' => $file->getMimeType(), 'data' => base64_encode($file->getData()), 'path' => $file->getIdentifier(), 'name' => 'text.txt']]])];

$file = new FileSystemFile($samples . 'datatypes/file/image-png_noname_data.png');
$returnValue[] = [$file, json_encode(['base' => ['file' => ['mime' => $file->getMimeType(), 'data' => base64_encode($file->getData())]]])];
$returnValue[] = [$file, json_encode(['base' => ['file' => ['mime' => $file->getMimeType(), 'data' => base64_encode($file->getData()), 'path' => $file->getIdentifier()]]])];

$id = 'http://some.cloud.storage/path/to/stored-file.txt';
$mimeType = 'text/plain';
Expand Down
4 changes: 4 additions & 0 deletions test/qtismtest/runtime/pci/json/JsonUnmarshallerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,10 @@ public function unmarshallFileProvider()
$file = $fileManager->retrieve($samples . 'datatypes/file/text-plain_text_data.txt');
$returnValue[] = [$file, '{ "base" : { "file" : { "mime" : "text\/plain", "data" : ' . json_encode(base64_encode('Some text...')) . ', "name" : "text.txt" } } }'];

$targetPath = $samples . 'datatypes/file/target_text-plain_text_data.txt';
$file = $fileManager->createFromData($file->getData(), $file->getMimeType(), $file->getFilename(), $targetPath);
$returnValue[] = [$file, '{ "base" : { "file" : { "mime" : "text\/plain", "data" : ' . json_encode(base64_encode('Some text...')) . ', "name" : "text.txt", "path" : "' . $targetPath . '" } } }'];

$originalfile = $samples . 'datatypes/file/raw/image.png';
$filepath = $samples . 'datatypes/file/image-png_noname_data.png';
$file = $fileManager->retrieve($filepath);
Expand Down
Loading