/** * @param ObjectManager $manager */ public function load(ObjectManager $manager) { $repo = $this->container->get('mautic.asset.model.asset')->getRepository(); $asset = new Asset(); $asset->setTitle('@TOCHANGE: Asset1 Title')->setAlias('asset1')->setOriginalFileName('@TOCHANGE: Asset1 Original File Name')->setPath('fdb8e28357b02d12d068de3e5661832e21bc08ec.doc')->setDownloadCount(1)->setUniqueDownloadCount(1)->setRevision(1)->setLanguage('en'); try { $repo->saveEntity($asset); } catch (\Exception $e) { echo $e->getMessage(); } }
/** * Validates file before upload * * @param ValidationEvent $event */ public function onUploadValidation(ValidationEvent $event) { $file = $event->getFile(); $extensions = $this->factory->getParameter('allowed_extensions'); $maxSize = Asset::convertSizeToBytes($this->factory->getParameter('max_size') . 'M'); // max size is set in MB if ($file !== null) { if ($file->getSize() > $maxSize) { $message = $this->translator->trans('mautic.asset.asset.error.file.size', array('%fileSize%' => round($file->getSize() / 1048576, 2), '%maxSize%' => round($maxSize / 1048576, 2)), 'validators'); throw new ValidationException($message); } if (!in_array(strtolower($file->getExtension()), array_map('strtolower', $extensions))) { $message = $this->translator->trans('mautic.asset.asset.error.file.extension', array('%fileExtension%' => $file->getExtension(), '%extensions%' => implode(', ', $extensions)), 'validators'); throw new ValidationException($message); } } }
/** * Validator to ensure proper data for the file fields * * @param Asset $object Entity object to validate * @param ExecutionContextInterface $context Context object */ public static function validateFile($object, ExecutionContextInterface $context) { if ($object->getStorageLocation() == 'local') { $tempName = $object->getTempName(); // If the object is stored locally, we should have file data if ($object->isNew() && $tempName === null) { $context->buildViolation('mautic.asset.asset.error.missing.file')->atPath('tempName')->setTranslationDomain('validators')->addViolation(); } if ($object->getTitle() === null) { $context->buildViolation('mautic.asset.asset.error.missing.title')->atPath('title')->setTranslationDomain('validators')->addViolation(); } // Unset any remote file data $object->setRemotePath(null); } elseif ($object->getStorageLocation() == 'remote') { // If the object is stored remotely, we should have a remote path if ($object->getRemotePath() === null) { $context->buildViolation('mautic.asset.asset.error.missing.remote.path')->atPath('remotePath')->setTranslationDomain('validators')->addViolation(); } // Unset any local file data $object->setPath(null); } }
/** * @param $assets * * @return int|string */ public function getTotalFilesize($assets) { $firstAsset = is_array($assets) ? reset($assets) : false; if ($assets instanceof PersistentCollection || is_object($firstAsset)) { $assetIds = array(); foreach ($assets as $asset) { $assetIds[] = $asset->getId(); } $assets = $assetIds; } if (!is_array($assets)) { $assets = array($assets); } if (empty($assets)) { return 0; } $repo = $this->getRepository(); $size = $repo->getAssetSize($assets); if ($size) { $size = Asset::convertBytesToHumanReadable($size); } return $size; }
/** * Generates edit form and processes post data * * @param int $objectId * @param bool $ignorePost * * @return JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response */ public function editAction($objectId, $ignorePost = false) { /** @var \Mautic\AssetBundle\Model\AssetModel $model */ $model = $this->factory->getModel('asset.asset'); /** @var \Mautic\AssetBundle\Entity\Asset $entity */ $entity = $model->getEntity($objectId); $entity->setMaxSize(Asset::convertSizeToBytes($this->factory->getParameter('max_size') . 'M')); // convert from MB to B $session = $this->factory->getSession(); $page = $this->factory->getSession()->get('mautic.asset.page', 1); $method = $this->request->getMethod(); $maxSize = $model->getMaxUploadSize(); $extensions = '.' . implode(', .', $this->factory->getParameter('allowed_extensions')); $maxSizeError = $this->get('translator')->trans('mautic.asset.asset.error.file.size', array('%fileSize%' => '{{filesize}}', '%maxSize%' => '{{maxFilesize}}'), 'validators'); $extensionError = $this->get('translator')->trans('mautic.asset.asset.error.file.extension.js', array('%extensions%' => $extensions), 'validators'); //set the return URL $returnUrl = $this->generateUrl('mautic_asset_index', array('page' => $page)); // Get upload folder $uploaderHelper = $this->container->get('oneup_uploader.templating.uploader_helper'); $uploadEndpoint = $uploaderHelper->endpoint('asset'); $postActionVars = array('returnUrl' => $returnUrl, 'viewParameters' => array('page' => $page), 'contentTemplate' => 'MauticAssetBundle:Asset:index', 'passthroughVars' => array('activeLink' => 'mautic_asset_index', 'mauticContent' => 'asset')); //not found if ($entity === null) { return $this->postActionRedirect(array_merge($postActionVars, array('flashes' => array(array('type' => 'error', 'msg' => 'mautic.asset.asset.error.notfound', 'msgVars' => array('%id%' => $objectId)))))); } elseif (!$this->factory->getSecurity()->hasEntityAccess('asset:assets:viewown', 'asset:assets:viewother', $entity->getCreatedBy())) { return $this->accessDenied(); } elseif ($model->isLocked($entity)) { //deny access if the entity is locked return $this->isLocked($postActionVars, $entity, 'asset.asset'); } // Create temporary asset ID $tempId = $method == 'POST' ? $this->request->request->get('asset[tempId]', '', true) : uniqid('tmp_'); $entity->setTempId($tempId); //Create the form $action = $this->generateUrl('mautic_asset_action', array('objectAction' => 'edit', 'objectId' => $objectId)); $form = $model->createForm($entity, $this->get('form.factory'), $action); ///Check for a submitted form and process it if (!$ignorePost && $method == 'POST') { $valid = false; if (!($cancelled = $this->isFormCancelled($form))) { if ($valid = $this->isFormValid($form)) { $entity->setUploadDir($this->factory->getParameter('upload_dir')); $entity->preUpload(); $entity->upload(); //form is valid so process the data $model->saveEntity($entity, $form->get('buttons')->get('save')->isClicked()); //remove the asset from request $this->request->files->remove('asset'); $this->addFlash('mautic.core.notice.updated', array('%name%' => $entity->getTitle(), '%menu_link%' => 'mautic_asset_index', '%url%' => $this->generateUrl('mautic_asset_action', array('objectAction' => 'edit', 'objectId' => $entity->getId())))); $returnUrl = $this->generateUrl('mautic_asset_action', array('objectAction' => 'view', 'objectId' => $entity->getId())); $viewParams = array('objectId' => $entity->getId()); $template = 'MauticAssetBundle:Asset:view'; } } else { //clear any modified content $session->remove('mautic.asestbuilder.' . $objectId . '.content'); //unlock the entity $model->unlockEntity($entity); $returnUrl = $this->generateUrl('mautic_asset_index', array('page' => $page)); $viewParams = array('page' => $page); $template = 'MauticAssetBundle:Asset:index'; } if ($cancelled || $valid && $form->get('buttons')->get('save')->isClicked()) { return $this->postActionRedirect(array_merge($postActionVars, array('returnUrl' => $returnUrl, 'viewParameters' => $viewParams, 'contentTemplate' => $template))); } } else { //lock the entity $model->lockEntity($entity); } // Check for integrations to cloud providers /** @var \Mautic\PluginBundle\Helper\IntegrationHelper $integrationHelper */ $integrationHelper = $this->factory->getHelper('integration'); $integrations = $integrationHelper->getIntegrationObjects(null, array('cloud_storage')); return $this->delegateView(array('viewParameters' => array('form' => $form->createView(), 'activeAsset' => $entity, 'assetDownloadUrl' => $model->generateUrl($entity), 'integrations' => $integrations, 'startOnLocal' => $entity->getStorageLocation() == 'local', 'uploadEndpoint' => $uploadEndpoint, 'maxSize' => $maxSize, 'maxSizeError' => $maxSizeError, 'extensions' => $extensions, 'extensionError' => $extensionError), 'contentTemplate' => 'MauticAssetBundle:Asset:form.html.php', 'passthroughVars' => array('activeLink' => '#mautic_asset_index', 'mauticContent' => 'asset', 'route' => $this->generateUrl('mautic_asset_action', array('objectAction' => 'edit', 'objectId' => $entity->getId()))))); }
public function getMaxUploadFileSize() { $max_upload = \Mautic\AssetBundle\Entity\Asset::getIniValue('upload_max_filesize'); $max_post = \Mautic\AssetBundle\Entity\Asset::getIniValue('post_max_size'); $memory_limit = \Mautic\AssetBundle\Entity\Asset::getIniValue('memory_limit'); $upload_mb = min(array_filter([$max_upload, $max_post, $memory_limit])); $this->__log(__METHOD__ . ' - max upload file size is ' . $upload_mb . 'Mb'); return $upload_mb; }
/** * {@inheritDoc} */ public function __toString() { $this->__initializer__ && $this->__initializer__->__invoke($this, '__toString', array()); return parent::__toString(); }