Skip to content

Commit a435543

Browse files
committed
Change saving local file url to DB. Add publicBaseUrl param to Module for full local files url
1 parent f91eb5c commit a435543

File tree

8 files changed

+75
-43
lines changed

8 files changed

+75
-43
lines changed

src/Module.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
* @property array $thumbStubUrls Default thumbnail stub urls according with file type.
2525
* @property bool $enableCsrfValidation Csrf validation.
2626
* @property string $defaultStorageType Default storage type. Can be 'local' or 's3'.
27+
* @property string $publicBaseUrl Public base project url, to set before media files url.
2728
* @property View $view View component to render content.
2829
*
2930
* @package Itstructure\MFUploader
@@ -126,6 +127,13 @@ class Module extends BaseModule
126127
*/
127128
public $defaultStorageType = self::STORAGE_TYPE_LOCAL;
128129

130+
/**
131+
* Public base project url, to set before media files url.
132+
*
133+
* @var string
134+
*/
135+
public $publicBaseUrl = '';
136+
129137
/**
130138
* View component to render content.
131139
*

src/components/LocalUploadComponent.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ class LocalUploadComponent extends BaseUploadComponent implements UploadComponen
3535
* @var string
3636
*/
3737
public $uploadDirs = [
38-
UploadModelInterface::FILE_TYPE_IMAGE => 'uploads'.DIRECTORY_SEPARATOR.'images',
39-
UploadModelInterface::FILE_TYPE_AUDIO => 'uploads'.DIRECTORY_SEPARATOR.'audio',
40-
UploadModelInterface::FILE_TYPE_VIDEO => 'uploads'.DIRECTORY_SEPARATOR.'video',
41-
UploadModelInterface::FILE_TYPE_APP => 'uploads'.DIRECTORY_SEPARATOR.'application',
42-
UploadModelInterface::FILE_TYPE_TEXT => 'uploads'.DIRECTORY_SEPARATOR.'text',
43-
UploadModelInterface::FILE_TYPE_OTHER => 'uploads'.DIRECTORY_SEPARATOR.'other',
38+
UploadModelInterface::FILE_TYPE_IMAGE => DIRECTORY_SEPARATOR.'uploads'.DIRECTORY_SEPARATOR.'images',
39+
UploadModelInterface::FILE_TYPE_AUDIO => DIRECTORY_SEPARATOR.'uploads'.DIRECTORY_SEPARATOR.'audio',
40+
UploadModelInterface::FILE_TYPE_VIDEO => DIRECTORY_SEPARATOR.'uploads'.DIRECTORY_SEPARATOR.'video',
41+
UploadModelInterface::FILE_TYPE_APP => DIRECTORY_SEPARATOR.'uploads'.DIRECTORY_SEPARATOR.'application',
42+
UploadModelInterface::FILE_TYPE_TEXT => DIRECTORY_SEPARATOR.'uploads'.DIRECTORY_SEPARATOR.'text',
43+
UploadModelInterface::FILE_TYPE_OTHER => DIRECTORY_SEPARATOR.'uploads'.DIRECTORY_SEPARATOR.'other',
4444
];
4545

4646
/**

src/controllers/FileinfoController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,11 @@ public function actionIndex()
7878
if ($model->isImage()) {
7979
$urlToSetFile = $model->getThumbUrl(Module::THUMB_ALIAS_MEDIUM);
8080
if (empty($urlToSetFile)) {
81-
$urlToSetFile = $model->url;
81+
$urlToSetFile = $model->getViewUrl();
8282
}
8383

8484
} else {
85-
$urlToSetFile = $model->url;
85+
$urlToSetFile = $model->getViewUrl();
8686
}
8787

8888
// Set width to set file by Java script.

src/controllers/upload/CommonUploadController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ private function getUploadResponse(): array
268268

269269
return [
270270
'id' => $this->uploadModel->getId(),
271-
'url' => $mediafileModel->url,
271+
'url' => $mediafileModel->getViewUrl(),
272272
'thumbnailUrl' => $mediafileModel->getDefaultThumbUrl(UploadmanagerAsset::register($this->view)->baseUrl),
273273
'name' => $mediafileModel->filename,
274274
'type' => $mediafileModel->type,

src/models/Mediafile.php

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ public function getImagePreview(array $mainTagOptions): string
295295
*/
296296
public function getAudioPreview(array $mainTagOptions): string
297297
{
298-
return Html::audio($this->url, ArrayHelper::merge([
298+
return Html::audio($this->getViewUrl(), ArrayHelper::merge([
299299
'source' => [
300300
'type' => $this->type
301301
]
@@ -312,7 +312,7 @@ public function getAudioPreview(array $mainTagOptions): string
312312
*/
313313
public function getVideoPreview(array $mainTagOptions): string
314314
{
315-
return Html::video($this->url, ArrayHelper::merge([
315+
return Html::video($this->getViewUrl(), ArrayHelper::merge([
316316
'source' => [
317317
'type' => $this->type
318318
]
@@ -406,7 +406,7 @@ public function getTextPreviewUrl($baseUrl = ''): string
406406
$root = DIRECTORY_SEPARATOR;
407407
}
408408

409-
return $root . $this->getModule()->thumbStubUrls[UploadModelInterface::FILE_TYPE_APP];
409+
return $root . $this->getModule()->thumbStubUrls[UploadModelInterface::FILE_TYPE_TEXT];
410410
}
411411

412412
/**
@@ -448,12 +448,19 @@ public function getThumbs(): array
448448
public function getThumbUrl(string $alias): string
449449
{
450450
if ($alias === Module::THUMB_ALIAS_ORIGINAL) {
451-
return $this->url;
451+
$url = $this->getViewUrl();
452+
453+
} else {
454+
$thumbs = $this->getThumbs();
455+
456+
$url = !empty($thumbs[$alias]) ? $thumbs[$alias] : '';
452457
}
453458

454-
$thumbs = $this->getThumbs();
459+
if (empty($url)) {
460+
return '';
461+
}
455462

456-
return !empty($thumbs[$alias]) ? $thumbs[$alias] : '';
463+
return $this->getViewUrl($url);
457464
}
458465

459466
/**
@@ -591,6 +598,31 @@ public function isWord(): bool
591598
return strpos($this->type, UploadModelInterface::FILE_TYPE_APP_WORD) !== false;
592599
}
593600

601+
/**
602+
* @param string $url
603+
*
604+
* @return string
605+
*/
606+
public function getFullPublicUrl(string $url): string
607+
{
608+
return rtrim(rtrim($this->getModule()->publicBaseUrl, '/'), '\\') . '/' . ltrim(str_replace('\\', '/', $url), '/');
609+
}
610+
611+
/**
612+
* If storage is local, public base url will be linked with a url from DB.
613+
* It is useful for html templates.
614+
*
615+
* @param string|null $url
616+
*
617+
* @return string
618+
*/
619+
public function getViewUrl(string $url = null): string
620+
{
621+
$url = empty($url) ? $this->url : $url;
622+
623+
return $this->storage == Module::STORAGE_TYPE_LOCAL ? $this->getFullPublicUrl($url) : $url;
624+
}
625+
594626
/**
595627
* Get preview option by location and file type.
596628
*

src/models/upload/LocalUpload.php

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function init()
5252
throw new InvalidConfigException('The uploadRoot is not defined correctly.');
5353
}
5454

55-
$this->uploadRoot = trim(trim($this->uploadRoot, '/'), '\\');
55+
$this->uploadRoot = rtrim(rtrim($this->uploadRoot, '/'), '\\');
5656
}
5757

5858
/**
@@ -79,25 +79,23 @@ protected function getStorageType(): string
7979
*/
8080
protected function setParamsForSend(): void
8181
{
82-
$uploadDir = trim(trim($this->getUploadDirConfig($this->file->type), '/'), '\\');
82+
$uploadDir = rtrim(rtrim($this->getUploadDirConfig($this->file->type), '/'), '\\');
8383

8484
if (!empty($this->subDir)) {
85-
$uploadDir = $uploadDir .
86-
DIRECTORY_SEPARATOR .
87-
trim(trim($this->subDir, '/'), '\\');
85+
$uploadDir = $uploadDir . DIRECTORY_SEPARATOR . trim(trim($this->subDir, '/'), '\\');
8886
}
8987

9088
$this->uploadDir = $uploadDir .
9189
DIRECTORY_SEPARATOR . substr(md5(time()), 0, self::DIR_LENGTH_FIRST) .
9290
DIRECTORY_SEPARATOR . substr(md5(microtime().$this->file->tempName), 0, self::DIR_LENGTH_SECOND);
9391

94-
$this->uploadPath = $this->uploadRoot . DIRECTORY_SEPARATOR . $this->uploadDir;
92+
$this->uploadPath = $this->uploadRoot . DIRECTORY_SEPARATOR . ltrim(ltrim($this->uploadDir, '/'), '\\');
9593

9694
$this->outFileName = $this->renameFiles ?
9795
md5(md5(microtime()).$this->file->tempName).'.'.$this->file->extension :
9896
Inflector::slug($this->file->baseName).'.'. $this->file->extension;
9997

100-
$this->databaseUrl = DIRECTORY_SEPARATOR . $this->uploadDir . DIRECTORY_SEPARATOR . $this->outFileName;
98+
$this->databaseUrl = $this->uploadDir . DIRECTORY_SEPARATOR . $this->outFileName;
10199
}
102100

103101
/**
@@ -109,17 +107,11 @@ protected function setParamsForSend(): void
109107
*/
110108
protected function setParamsForDelete(): void
111109
{
112-
$originalFile = pathinfo($this->mediafileModel->url);
113-
114-
$dirname = ltrim($originalFile['dirname'], DIRECTORY_SEPARATOR);
110+
$originalFile = pathinfo($this->uploadRoot . DIRECTORY_SEPARATOR . ltrim(ltrim($this->mediafileModel->url, '\\'), '/'));
115111

116-
$dirnameParent = substr($dirname, 0, -(self::DIR_LENGTH_SECOND+1));
112+
$dirnameParent = substr($originalFile['dirname'], 0, -(self::DIR_LENGTH_SECOND + 1));
117113

118-
if (count(BaseFileHelper::findDirectories($dirnameParent)) == 1) {
119-
$this->directoryForDelete = $this->uploadRoot . DIRECTORY_SEPARATOR . $dirnameParent;
120-
} else {
121-
$this->directoryForDelete = $this->uploadRoot . DIRECTORY_SEPARATOR . $dirname;
122-
}
114+
$this->directoryForDelete = count(BaseFileHelper::findDirectories($dirnameParent)) == 1 ? $dirnameParent : $originalFile['dirname'];
123115
}
124116

125117
/**
@@ -160,19 +152,19 @@ protected function createThumb(ThumbConfigInterface $thumbConfig)
160152
$originalFile = pathinfo($this->mediafileModel->url);
161153

162154
$thumbUrl = $originalFile['dirname'] .
163-
DIRECTORY_SEPARATOR .
164-
$this->getThumbFilename($originalFile['filename'],
165-
$originalFile['extension'],
166-
$thumbConfig->alias,
167-
$thumbConfig->width,
168-
$thumbConfig->height
169-
);
170-
171-
Image::thumbnail($this->uploadRoot . DIRECTORY_SEPARATOR . $this->mediafileModel->url,
155+
DIRECTORY_SEPARATOR .
156+
$this->getThumbFilename($originalFile['filename'],
157+
$originalFile['extension'],
158+
$thumbConfig->alias,
159+
$thumbConfig->width,
160+
$thumbConfig->height
161+
);
162+
163+
Image::thumbnail($this->uploadRoot . DIRECTORY_SEPARATOR . ltrim(ltrim($this->mediafileModel->url, '\\'), '/'),
172164
$thumbConfig->width,
173165
$thumbConfig->height,
174166
$thumbConfig->mode
175-
)->save($this->uploadRoot . DIRECTORY_SEPARATOR . trim(trim($thumbUrl, '\\'), '/'));
167+
)->save($this->uploadRoot . DIRECTORY_SEPARATOR . ltrim(ltrim($thumbUrl, '\\'), '/'));
176168

177169
return $thumbUrl;
178170
}

src/views/albums/_existing-mediafiles.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
'externalTag' => [
4141
'name' => 'a',
4242
'options' => [
43-
'href' => $mediafile->url,
43+
'href' => $mediafile->getViewUrl(),
4444
'target' => '_blank'
4545
]
4646
]

src/views/albums/view.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@
115115
'externalTag' => [
116116
'name' => 'a',
117117
'options' => [
118-
'href' => $mediafile->url,
118+
'href' => $mediafile->getViewUrl(),
119119
'target' => '_blank'
120120
]
121121
]

0 commit comments

Comments
 (0)