23
23
namespace Xibo\Entity;
26
use GuzzleHttp\Client;
27
use GuzzleHttp\Exception\RequestException;
26
28
use Respect\Validation\Validator as v;
27
29
use Xibo\Exception\ConfigurationException;
28
use Xibo\Exception\DuplicateEntityException;
29
use Xibo\Exception\InvalidArgumentException;
30
30
use Xibo\Exception\NotFoundException;
31
use Xibo\Exception\XiboException;
32
use Xibo\Factory\DisplayFactory;
33
31
use Xibo\Factory\DisplayGroupFactory;
34
32
use Xibo\Factory\LayoutFactory;
35
33
use Xibo\Factory\MediaFactory;
36
34
use Xibo\Factory\PermissionFactory;
37
35
use Xibo\Factory\PlaylistFactory;
38
use Xibo\Factory\ScheduleFactory;
39
36
use Xibo\Factory\TagFactory;
40
37
use Xibo\Factory\WidgetFactory;
41
38
use Xibo\Service\ConfigServiceInterface;
261
251
* @param LayoutFactory $layoutFactory
262
252
* @param WidgetFactory $widgetFactory
263
253
* @param DisplayGroupFactory $displayGroupFactory
264
* @param DisplayFactory $displayFactory
265
* @param ScheduleFactory $scheduleFactory
268
public function setChildObjectDependencies($layoutFactory, $widgetFactory, $displayGroupFactory, $displayFactory, $scheduleFactory)
256
public function setChildObjectDependencies($layoutFactory, $widgetFactory, $displayGroupFactory)
270
258
$this->layoutFactory = $layoutFactory;
271
259
$this->widgetFactory = $widgetFactory;
272
260
$this->displayGroupFactory = $displayGroupFactory;
273
$this->displayFactory = $displayFactory;
274
$this->scheduleFactory = $scheduleFactory;
410
384
* @param array $options
411
* @throws XiboException
413
386
public function validate($options)
415
388
if (!v::string()->notEmpty()->validate($this->mediaType))
416
throw new InvalidArgumentException(__('Unknown Module Type'), 'type');
389
throw new \InvalidArgumentException(__('Unknown Module Type'));
418
391
if (!v::string()->notEmpty()->length(1, 100)->validate($this->name))
419
throw new InvalidArgumentException(__('The name must be between 1 and 100 characters'), 'name');
392
throw new \InvalidArgumentException(__('The name must be between 1 and 100 characters'));
421
394
// Check the naming of this item to ensure it doesn't conflict
422
395
$params = array();
502
468
if ($this->mediaId == null || $this->mediaId == 0) {
505
// Always set force to true as we always want to save new files
506
$this->isSaveRequired = true;
472
// If the media file is invalid, then force an update (only applies to module files)
473
if ($this->valid == 0)
511
// If the media file is invalid, then force an update (only applies to module files)
512
$expires = $this->getOriginalValue('expires');
513
$this->isSaveRequired = ($this->isSaveRequired || $this->valid == 0 || ($expires > 0 && $expires < time()));
516
if ($options['deferred']) {
517
$this->getLog()->debug('Media Update deferred until later');
519
$this->getLog()->debug('Media Update happening now');
522
if ($this->isSaveRequired)
600
539
// Swap any audio nodes over to this new widget media assignment.
601
540
$this->getStore()->update('
602
UPDATE `lkwidgetaudio` SET mediaId = :mediaId WHERE widgetId = :widgetId AND mediaId = :oldMediaId
541
UPDATE `lkwidgetaudio` SET mediaId = :mediaId WHERE widgetId = :widgetId AND oldMediaId = :oldMediaId
604
543
'mediaId' => $parentMedia->mediaId,
605
544
'widgetId' => $widget->widgetId,
622
561
foreach ($this->displayGroups as $displayGroup) {
623
562
/* @var \Xibo\Entity\DisplayGroup $displayGroup */
624
$displayGroup->setChildObjectDependencies($this->displayFactory, $this->layoutFactory, $this->mediaFactory, $this->scheduleFactory);
625
563
$displayGroup->unassignMedia($this);
627
565
if ($parentMedia != null)
660
598
private function add()
662
600
$this->mediaId = $this->getStore()->insert('
663
INSERT INTO `media` (`name`, `type`, duration, originalFilename, userID, retired, moduleSystemFile, released, apiRef, valid)
664
VALUES (:name, :type, :duration, :originalFileName, :userId, :retired, :moduleSystemFile, :released, :apiRef, :valid)
601
INSERT INTO media (name, type, duration, originalFilename, userID, retired, moduleSystemFile, expires, released, apiRef)
602
VALUES (:name, :type, :duration, :originalFileName, :userId, :retired, :moduleSystemFile, :expires, :released, :apiRef)
666
604
'name' => $this->name,
667
605
'type' => $this->mediaType,
670
608
'userId' => $this->ownerId,
671
609
'retired' => $this->retired,
672
610
'moduleSystemFile' => (($this->moduleSystemFile) ? 1 : 0),
611
'expires' => $this->expires,
673
612
'released' => $this->released,
674
'apiRef' => $this->apiRef,
613
'apiRef' => $this->apiRef
618
// Update the MD5 and storedAs to suit
619
$this->getStore()->update('UPDATE `media` SET md5 = :md5, fileSize = :fileSize, storedAs = :storedAs, duration = :duration WHERE mediaId = :mediaId', [
620
'fileSize' => $this->fileSize,
622
'storedAs' => $this->storedAs,
623
'duration' => $this->duration,
624
'mediaId' => $this->mediaId
683
632
private function edit()
634
// Do we need to pull a new update?
635
// Is the file either expired or is force set
636
if ($this->force || ($this->expires > 0 && $this->expires < time())) {
637
$this->getLog()->debug('Media %s has expired: %s. Force = %d', $this->name, date('Y-m-d H:i', $this->expires), $this->force);
685
641
$this->getStore()->update('
688
644
duration = :duration,
689
645
retired = :retired,
647
filesize = :fileSize,
690
649
moduleSystemFile = :moduleSystemFile,
691
650
editedMediaId = :editedMediaId,
692
651
isEdited = :isEdited,
698
657
'name' => $this->name,
699
658
'duration' => $this->duration,
700
659
'retired' => $this->retired,
660
'fileSize' => $this->fileSize,
662
'expires' => $this->expires,
701
663
'moduleSystemFile' => $this->moduleSystemFile,
702
664
'editedMediaId' => $this->parentId,
703
665
'isEdited' => $this->isEdited,
712
674
* Save File to Library
713
* works on files that are already in the File system
675
* this should download remote files, handle clones, handle local module files and also handle files uploaded
714
677
* @throws ConfigurationException
716
public function saveFile()
679
private function saveFile()
681
// If we are a remote media item, we want to download the newFile and save it to a temporary location
682
if ($this->isRemote) {
718
686
$libraryFolder = $this->config->GetSetting('LIBRARY_LOCATION');
720
688
// Work out the extension
721
$lastPeriod = strrchr($this->fileName, '.');
723
// Determine the save name
724
if ($lastPeriod === false) {
725
$saveName = $this->mediaId;
727
$saveName = $this->mediaId . '.' . strtolower(substr($lastPeriod, 1));
730
$this->getLog()->debug('saveFile for "%s" with storedAs = "%s", fileName = "%s" to "%s". Always Copy = "%s", Cloned = "%s"',
689
$extension = strtolower(substr(strrchr($this->fileName, '.'), 1));
691
$this->getLog()->debug('saveFile for "%s" with storedAs = "%s" (empty = %s), fileName = "%s" to "%s". Always Copy = "%s", Cloned = "%s"',
694
empty($this->storedAs),
696
$this->mediaId . '.' . $extension,
735
697
$this->alwaysCopy,
743
705
if ($this->cloned) {
744
706
$this->getLog()->debug('Copying cloned file');
745
707
// Copy the file into the library
746
if (!@copy($libraryFolder . $this->fileName, $libraryFolder . $saveName))
708
if (!@copy($libraryFolder . $this->fileName, $libraryFolder . $this->mediaId . '.' . $extension))
747
709
throw new ConfigurationException(__('Problem copying file in the Library Folder'));
750
712
$this->getLog()->debug('Moving temporary file');
751
713
// Move the file into the library
752
if (!@rename($libraryFolder . 'temp/' . $this->fileName, $libraryFolder . $saveName))
714
if (!@rename($libraryFolder . 'temp/' . $this->fileName, $libraryFolder . $this->mediaId . '.' . $extension))
753
715
throw new ConfigurationException(__('Problem moving uploaded file into the Library Folder'));
756
718
// Set the storedAs
757
$this->storedAs = $saveName;
719
$this->storedAs = $this->mediaId . '.' . $extension;
722
$this->getLog()->debug('Copying specified file');
760
723
// We have pre-defined where we want this to be stored
761
724
if (empty($this->storedAs)) {
762
725
// Assume we want to set this automatically (i.e. we are set to always copy)
763
$this->storedAs = $saveName;
726
$this->storedAs = $this->mediaId . '.' . $extension;
766
if ($this->isRemote) {
767
$this->getLog()->debug('Moving temporary file');
769
// Move the file into the library
770
if (!@rename($libraryFolder . 'temp/' . $this->fileName, $libraryFolder . $this->storedAs))
771
throw new ConfigurationException(__('Problem moving downloaded file into the Library Folder'));
773
$this->getLog()->debug('Copying specified file');
775
if (!@copy($this->fileName, $libraryFolder . $this->storedAs)) {
776
$this->getLog()->error('Cannot copy %s to %s', $this->fileName, $libraryFolder . $this->storedAs);
777
throw new ConfigurationException(__('Problem copying provided file into the Library Folder'));
729
if (!@copy($this->fileName, $libraryFolder . $this->storedAs)) {
730
$this->getLog()->error('Cannot copy %s to %s', $this->fileName, $libraryFolder . $this->storedAs);
731
throw new ConfigurationException(__('Problem moving provided file into the Library Folder'));
782
735
// Work out the MD5
783
736
$this->md5 = md5_file($libraryFolder . $this->storedAs);
784
737
$this->fileSize = filesize($libraryFolder . $this->storedAs);
786
// Update the MD5 and storedAs to suit
787
$this->getStore()->update('UPDATE `media` SET md5 = :md5, fileSize = :fileSize, storedAs = :storedAs, expires = :expires, valid = 1 WHERE mediaId = :mediaId', [
788
'fileSize' => $this->fileSize,
790
'storedAs' => $this->storedAs,
791
'expires' => $this->expires,
792
'mediaId' => $this->mediaId
835
public function downloadUrl()
837
return $this->fileName;
844
public function downloadSink()
846
return $this->config->GetSetting('LIBRARY_LOCATION') . 'temp' . DIRECTORY_SEPARATOR . $this->name;
776
* Download remote file
778
private function download()
780
if (!$this->isRemote || $this->fileName == '')
781
throw new \InvalidArgumentException(__('Not in a suitable state to download'));
783
// Open the temporary file
784
$storedAs = $this->config->GetSetting('LIBRARY_LOCATION') . 'temp' . DIRECTORY_SEPARATOR . $this->name;
786
$this->getLog()->debug('Downloading %s to %s', $this->fileName, $storedAs);
788
if (!$fileHandle = fopen($storedAs, 'w'))
789
throw new ConfigurationException(__('Temporary location not writable'));
792
$client = new Client();
793
$client->get($this->fileName, $this->config->getGuzzleProxy(['save_to' => $fileHandle]));
795
catch (RequestException $e) {
796
$this->getLog()->error('Unable to get %s, %s', $this->fileName, $e->getMessage());
799
// Change the filename to our temporary file
800
$this->fileName = $storedAs;
b'\\ No newline at end of file'