492
490
if ($this->mediaId == null || $this->mediaId == 0) {
493
$promise = $this->add();
493
// Always set force to true as we always want to save new files
494
$this->isSaveRequired = true;
496
499
// If the media file is invalid, then force an update (only applies to module files)
497
if ($this->valid == 0)
500
$promise = $this->edit();
500
$expires = $this->getOriginalValue('expires');
501
$this->isSaveRequired = ($this->valid == 0 || ($expires > 0 && $expires < time()));
503
if (!$options['deferred']) {
504
$this->getLog()->debug('Handling promise immediately.');
504
if ($options['deferred']) {
505
$this->getLog()->debug('Media Update deferred until later');
507
$this->getLog()->debug('Async Upload, returning promise');
507
$this->getLog()->debug('Media Update happening now');
510
if ($this->isSaveRequired)
641
* @return PromiseInterface
642
645
* @throws ConfigurationException
644
647
private function add()
646
649
$this->mediaId = $this->getStore()->insert('
647
INSERT INTO media (name, type, duration, originalFilename, userID, retired, moduleSystemFile, expires, released, apiRef)
648
VALUES (:name, :type, :duration, :originalFileName, :userId, :retired, :moduleSystemFile, :expires, :released, :apiRef)
650
INSERT INTO `media` (`name`, `type`, duration, originalFilename, userID, retired, moduleSystemFile, released, apiRef, valid)
651
VALUES (:name, :type, :duration, :originalFileName, :userId, :retired, :moduleSystemFile, :released, :apiRef, :valid)
650
653
'name' => $this->name,
651
654
'type' => $this->mediaType,
654
657
'userId' => $this->ownerId,
655
658
'retired' => $this->retired,
656
659
'moduleSystemFile' => (($this->moduleSystemFile) ? 1 : 0),
657
'expires' => $this->expires,
658
660
'released' => $this->released,
659
'apiRef' => $this->apiRef
661
'apiRef' => $this->apiRef,
666
$promise = new Promise(function() use (&$promise) {
667
$promise->resolve('Waited');
669
$promise->then(function ($value) use (&$media) {
670
$media->getLog()->debug('Media save promise resolved ' . $value);
674
// Update the MD5 and storedAs to suit
675
$media->getStore()->update('UPDATE `media` SET md5 = :md5, fileSize = :fileSize, storedAs = :storedAs, duration = :duration WHERE mediaId = :mediaId', [
676
'fileSize' => $this->fileSize,
678
'storedAs' => $this->storedAs,
679
'duration' => $this->duration,
680
'mediaId' => $this->mediaId
683
}, function ($reason) use (&$media) {
684
// Promise rejected - that's bad.
685
$media->getLog()->error('Unable to download media because ' . $reason);
693
* @return PromiseInterface
694
668
* @throws ConfigurationException
696
670
private function edit()
702
$promise = new Promise(function() use (&$promise) {
703
$promise->resolve('Waited');
705
$promise->then(function ($value) use (&$media) {
706
$media->getLog()->debug('Media save promise resolved ' . $value);
708
// Do we need to pull a new update?
709
// Is the file either expired or is force set
710
if ($media->force || ($media->expires > 0 && $media->expires < time())) {
711
$media->getLog()->debug('Media %s has expired: %s. Force = %d', $media->name, date('Y-m-d H:i', $media->expires), $media->force);
716
$media->getStore()->update('
719
duration = :duration,
722
filesize = :fileSize,
724
moduleSystemFile = :moduleSystemFile,
725
editedMediaId = :editedMediaId,
726
isEdited = :isEdited,
728
released = :released,
730
WHERE mediaId = :mediaId
732
'name' => $this->name,
733
'duration' => $this->duration,
734
'retired' => $this->retired,
735
'fileSize' => $this->fileSize,
737
'expires' => $this->expires,
738
'moduleSystemFile' => $this->moduleSystemFile,
739
'editedMediaId' => $this->parentId,
740
'isEdited' => $this->isEdited,
741
'userId' => $this->ownerId,
742
'released' => $this->released,
743
'apiRef' => $this->apiRef,
744
'mediaId' => $this->mediaId
747
}, function ($reason) use (&$media) {
748
// Promise rejected - that's bad.
749
$media->getLog()->error('Unable to download media because ' . $reason);
672
$this->getStore()->update('
675
duration = :duration,
677
moduleSystemFile = :moduleSystemFile,
678
editedMediaId = :editedMediaId,
679
isEdited = :isEdited,
681
released = :released,
683
WHERE mediaId = :mediaId
685
'name' => $this->name,
686
'duration' => $this->duration,
687
'retired' => $this->retired,
688
'moduleSystemFile' => $this->moduleSystemFile,
689
'editedMediaId' => $this->parentId,
690
'isEdited' => $this->isEdited,
691
'userId' => $this->ownerId,
692
'released' => $this->released,
693
'apiRef' => $this->apiRef,
694
'mediaId' => $this->mediaId
756
699
* Save File to Library
757
* this should download remote files, handle clones, handle local module files and also handle files uploaded
700
* works on files that are already in the File system
759
701
* @throws ConfigurationException
761
703
public function saveFile()
763
// If we are a remote media item, we want to download the newFile and save it to a temporary location
764
if ($this->isRemote) {
768
705
$libraryFolder = $this->config->GetSetting('LIBRARY_LOCATION');
770
707
// Work out the extension
771
708
$extension = strtolower(substr(strrchr($this->fileName, '.'), 1));
773
$this->getLog()->debug('saveFile for "%s" with storedAs = "%s" (empty = %s), fileName = "%s" to "%s". Always Copy = "%s", Cloned = "%s"',
710
$this->getLog()->debug('saveFile for "%s" with storedAs = "%s", fileName = "%s" to "%s". Always Copy = "%s", Cloned = "%s"',
776
empty($this->storedAs),
778
714
$this->mediaId . '.' . $extension,
779
715
$this->alwaysCopy,
801
737
$this->storedAs = $this->mediaId . '.' . $extension;
804
$this->getLog()->debug('Copying specified file');
805
740
// We have pre-defined where we want this to be stored
806
741
if (empty($this->storedAs)) {
807
742
// Assume we want to set this automatically (i.e. we are set to always copy)
808
743
$this->storedAs = $this->mediaId . '.' . $extension;
811
if (!@copy($this->fileName, $libraryFolder . $this->storedAs)) {
812
$this->getLog()->error('Cannot copy %s to %s', $this->fileName, $libraryFolder . $this->storedAs);
813
throw new ConfigurationException(__('Problem moving provided file into the Library Folder'));
746
if ($this->isRemote) {
747
$this->getLog()->debug('Moving temporary file');
749
// Move the file into the library
750
if (!@rename($libraryFolder . 'temp/' . $this->fileName, $libraryFolder . $this->storedAs))
751
throw new ConfigurationException(__('Problem moving downloaded file into the Library Folder'));
753
$this->getLog()->debug('Copying specified file');
755
if (!@copy($this->fileName, $libraryFolder . $this->storedAs)) {
756
$this->getLog()->error('Cannot copy %s to %s', $this->fileName, $libraryFolder . $this->storedAs);
757
throw new ConfigurationException(__('Problem copying provided file into the Library Folder'));
817
762
// Work out the MD5
818
763
$this->md5 = md5_file($libraryFolder . $this->storedAs);
819
764
$this->fileSize = filesize($libraryFolder . $this->storedAs);
766
// Update the MD5 and storedAs to suit
767
$this->getStore()->update('UPDATE `media` SET md5 = :md5, fileSize = :fileSize, storedAs = :storedAs, expires = :expires, valid = 1 WHERE mediaId = :mediaId', [
768
'fileSize' => $this->fileSize,
770
'storedAs' => $this->storedAs,
771
'expires' => $this->expires,
772
'mediaId' => $this->mediaId
858
* Download remote file
860
private function download()
862
if (!$this->isRemote || $this->fileName == '')
863
throw new \InvalidArgumentException(__('Not in a suitable state to download'));
865
// Open the temporary file
866
$storedAs = $this->config->GetSetting('LIBRARY_LOCATION') . 'temp' . DIRECTORY_SEPARATOR . $this->name;
868
$this->getLog()->debug('Downloading %s to %s', $this->fileName, $storedAs);
870
if (!$fileHandle = fopen($storedAs, 'w'))
871
throw new ConfigurationException(__('Temporary location not writable'));
874
$client = new Client();
875
$client->get($this->fileName, $this->config->getGuzzleProxy(['save_to' => $fileHandle]));
877
catch (RequestException $e) {
878
$this->getLog()->error('Unable to get %s, %s', $this->fileName, $e->getMessage());
881
// Change the filename to our temporary file
882
$this->fileName = $storedAs;
815
public function downloadUrl()
817
return $this->fileName;
824
public function downloadSink()
826
return $this->config->GetSetting('LIBRARY_LOCATION') . 'temp' . DIRECTORY_SEPARATOR . $this->name;
b'\\ No newline at end of file'