18 private $mediaDirectory;
23 private $uploaderFactory;
38 private $entityTypeCode;
43 private $allowedExtensions = [];
61 \
Magento\MediaStorage\Model\File\UploaderFactory $uploaderFactory,
65 \
Magento\Framework\File\Mime $mime,
66 array $allowedExtensions = []
68 $this->mediaDirectory =
$filesystem->getDirectoryWrite(\
Magento\Framework\
App\Filesystem\DirectoryList::MEDIA);
69 $this->uploaderFactory = $uploaderFactory;
70 $this->urlBuilder = $urlBuilder;
71 $this->urlEncoder = $urlEncoder;
72 $this->entityTypeCode = $entityTypeCode;
74 $this->allowedExtensions = $allowedExtensions;
85 $filePath = $this->entityTypeCode .
'/' . ltrim(
$fileName,
'/');
87 $fileContent = $this->mediaDirectory->readFile($filePath);
89 $encodedContent = base64_encode($fileContent);
90 return $encodedContent;
101 $filePath = $this->entityTypeCode .
'/' . ltrim(
$fileName,
'/');
103 $result = $this->mediaDirectory->stat($filePath);
115 $filePath = $this->entityTypeCode .
'/' . ltrim(
$fileName,
'/');
116 $absoluteFilePath = $this->mediaDirectory->getAbsolutePath($filePath);
118 $result = $this->mime->getMimeType($absoluteFilePath);
130 $filePath = $this->entityTypeCode .
'/' . ltrim(
$fileName,
'/');
132 $result = $this->mediaDirectory->isExist($filePath);
148 $filePath = $this->entityTypeCode .
'/' . ltrim($filePath,
'/');
150 . $this->mediaDirectory->getRelativePath($filePath);
154 $viewUrl = $this->urlBuilder->getUrl(
155 'customer/index/viewfile',
156 [
$type => $this->urlEncoder->encode(ltrim($filePath,
'/'))]
170 public function saveTemporaryFile($fileId)
173 $uploader = $this->uploaderFactory->create([
'fileId' => $fileId]);
174 $uploader->setFilesDispersion(
false);
175 $uploader->setFilenamesCaseSensitivity(
false);
176 $uploader->setAllowRenameFiles(
true);
177 $uploader->setAllowedExtensions($this->allowedExtensions);
179 $path = $this->mediaDirectory->getAbsolutePath(
180 $this->entityTypeCode .
'/' . self::TMP_DIR
186 throw new \Magento\Framework\Exception\LocalizedException(
187 __(
'File can not be saved to the destination folder.')
206 $destinationPath = $this->entityTypeCode . $dispersionPath;
208 if (!$this->mediaDirectory->create($destinationPath)) {
209 throw new \Magento\Framework\Exception\LocalizedException(
210 __(
'Unable to create directory %1.', $destinationPath)
214 if (!$this->mediaDirectory->isWritable($destinationPath)) {
215 throw new \Magento\Framework\Exception\LocalizedException(
216 __(
'Destination folder is not writable or does not exists.')
221 $this->mediaDirectory->getAbsolutePath($destinationPath) .
'/' .
$fileName 225 $this->mediaDirectory->renameFile(
226 $this->entityTypeCode .
'/' . self::TMP_DIR .
'/' .
$fileName,
227 $destinationPath .
'/' . $destinationFileName
229 }
catch (\Exception $e) {
230 throw new \Magento\Framework\Exception\LocalizedException(
231 __(
'Something went wrong while saving the file.')
247 $filePath = $this->entityTypeCode .
'/' . ltrim(
$fileName,
'/');
249 $result = $this->mediaDirectory->delete($filePath);
getBase64EncodedData($fileName)
getViewUrl($filePath, $type)
__construct(\Magento\Framework\Filesystem $filesystem, \Magento\MediaStorage\Model\File\UploaderFactory $uploaderFactory, \Magento\Framework\UrlInterface $urlBuilder, \Magento\Framework\Url\EncoderInterface $urlEncoder, $entityTypeCode, \Magento\Framework\File\Mime $mime, array $allowedExtensions=[])
moveTemporaryFile($fileName)
removeUploadedFile($fileName)
static getNewFileName($destinationFile)
static getDispersionPath($fileName)