50
common/Files/Traits/GetsEntryTypeFromMime.php
Executable file
50
common/Files/Traits/GetsEntryTypeFromMime.php
Executable file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Files\Traits;
|
||||
|
||||
use Str;
|
||||
|
||||
trait GetsEntryTypeFromMime
|
||||
{
|
||||
protected function getTypeFromMime(
|
||||
string $mime,
|
||||
string $extension = null,
|
||||
): string {
|
||||
$default = explode('/', $mime)[0];
|
||||
|
||||
if ($mime === 'text/plain' && $extension === 'csv') {
|
||||
return 'spreadsheet';
|
||||
}
|
||||
|
||||
switch ($mime) {
|
||||
case 'application/x-zip-compressed':
|
||||
case 'application/zip':
|
||||
return 'archive';
|
||||
case 'application/pdf':
|
||||
return 'pdf';
|
||||
case 'image/svg':
|
||||
return 'image/svg+xml';
|
||||
case 'image/vnd.dwg':
|
||||
return 'file';
|
||||
case 'vnd.android.package-archive':
|
||||
return 'android package';
|
||||
case Str::contains($mime, ['xls', 'excel', 'spreadsheetml', 'csv']):
|
||||
return 'spreadsheet';
|
||||
case Str::contains($mime, 'photoshop'):
|
||||
return 'photoshop';
|
||||
case Str::contains($mime, 'officedocument.presentation'):
|
||||
return 'powerPoint';
|
||||
case Str::contains($mime, [
|
||||
'application/msword',
|
||||
'wordprocessingml.document',
|
||||
]):
|
||||
return 'word';
|
||||
case Str::contains($mime, ['postscript', 'x-eps']):
|
||||
return 'postscript';
|
||||
case Str::startsWith($mime, 'message/rfc'):
|
||||
return 'text/plain';
|
||||
default:
|
||||
return $default === 'application' ? 'file' : $default;
|
||||
}
|
||||
}
|
||||
}
|
||||
113
common/Files/Traits/HandlesEntryPaths.php
Executable file
113
common/Files/Traits/HandlesEntryPaths.php
Executable file
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Files\Traits;
|
||||
|
||||
use DB;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
trait HandlesEntryPaths
|
||||
{
|
||||
public function getPathAttribute($value): string
|
||||
{
|
||||
if (!$value) {
|
||||
$value = '';
|
||||
}
|
||||
|
||||
$parts = explode('/', $value);
|
||||
|
||||
$parts = array_map(function ($part) {
|
||||
return $this->decodePathId($part);
|
||||
}, array_filter($parts));
|
||||
|
||||
return implode('/', $parts);
|
||||
}
|
||||
|
||||
public function setPathAttribute($value)
|
||||
{
|
||||
if (!$value) {
|
||||
$value = '';
|
||||
}
|
||||
|
||||
$this->attributes['path'] = $this->encodePath($value);
|
||||
}
|
||||
|
||||
public function updatePaths(
|
||||
string $oldPath,
|
||||
string $newPath,
|
||||
$entryIds = null,
|
||||
): void {
|
||||
$oldPath = $this->encodePath($oldPath);
|
||||
$newPath = $this->encodePath($newPath);
|
||||
|
||||
$query = $this->newQuery();
|
||||
|
||||
if ($entryIds) {
|
||||
$query->whereIn('id', $entryIds);
|
||||
}
|
||||
|
||||
$query->where('path', 'LIKE', "$oldPath%")->update([
|
||||
'path' => DB::raw("REPLACE(path, '$oldPath', '$newPath')"),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads current model as well as all children.
|
||||
*/
|
||||
public function scopeAllChildren(Builder $builder): Builder
|
||||
{
|
||||
return $builder->where('path', 'like', $this->attributes['path'] . '%');
|
||||
}
|
||||
|
||||
public function getParentIds(): array
|
||||
{
|
||||
$folderIds = explode('/', $this->path);
|
||||
array_pop($folderIds);
|
||||
return $folderIds;
|
||||
}
|
||||
|
||||
public function scopeAllParents(Builder $builder): Builder
|
||||
{
|
||||
return $builder->whereIn('id', $this->getParentIds())->orderBy('path');
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate full path for current entry, based on its parent.
|
||||
*/
|
||||
public function generatePath(): void
|
||||
{
|
||||
if (!$this->exists) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->path = $this->id;
|
||||
|
||||
if ($this->parent_id && ($parent = $this->find($this->parent_id))) {
|
||||
$this->path = "{$parent->path}/$this->path";
|
||||
}
|
||||
|
||||
$this->save();
|
||||
}
|
||||
|
||||
private function encodePath($path): string
|
||||
{
|
||||
$parts = explode('/', (string) $path);
|
||||
|
||||
$parts = array_filter($parts);
|
||||
|
||||
$parts = array_map(function ($part) {
|
||||
return $this->encodePathId($part);
|
||||
}, $parts);
|
||||
|
||||
return implode('/', $parts);
|
||||
}
|
||||
|
||||
private function encodePathId($id): string
|
||||
{
|
||||
return base_convert($id, 10, 36);
|
||||
}
|
||||
|
||||
private function decodePathId($id): string
|
||||
{
|
||||
return base_convert($id, 36, 10);
|
||||
}
|
||||
}
|
||||
22
common/Files/Traits/HashesId.php
Executable file
22
common/Files/Traits/HashesId.php
Executable file
@@ -0,0 +1,22 @@
|
||||
<?php namespace Common\Files\Traits;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
trait HashesId
|
||||
{
|
||||
public function getHashAttribute(): string
|
||||
{
|
||||
return trim(base64_encode(str_pad($this->getRawOriginal('id').'|', 10, 'padding')), '=');
|
||||
}
|
||||
|
||||
public function scopeWhereHash(Builder $query, $value)
|
||||
{
|
||||
$id = $this->decodeHash($value);
|
||||
return $query->where('id', $id);
|
||||
}
|
||||
|
||||
public function decodeHash($hash): int
|
||||
{
|
||||
return (int) explode('|', base64_decode($hash))[0];
|
||||
}
|
||||
}
|
||||
49
common/Files/Traits/LoadsAllChildEntries.php
Executable file
49
common/Files/Traits/LoadsAllChildEntries.php
Executable file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Files\Traits;
|
||||
|
||||
use Common\Files\FileEntry;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
trait LoadsAllChildEntries
|
||||
{
|
||||
/**
|
||||
* Fetch all children of specified entries.
|
||||
*
|
||||
* @param array|Collection $entries
|
||||
* @param bool $withTrashed
|
||||
* @return Collection
|
||||
*/
|
||||
protected function loadChildEntries($entries, $withTrashed = false)
|
||||
{
|
||||
$builder = FileEntry::select(['id', 'file_name', 'type']);
|
||||
|
||||
if (is_array($entries)) {
|
||||
$entries = collect($entries);
|
||||
}
|
||||
|
||||
// load parent entries, if we got only IDs passed in
|
||||
if (is_numeric($entries->first())) {
|
||||
$entries = FileEntry::whereIn('id', $entries)->get();
|
||||
}
|
||||
|
||||
if ($withTrashed) {
|
||||
$builder->withTrashed();
|
||||
}
|
||||
|
||||
$entries->each(function (FileEntry $entry) use ($builder) {
|
||||
if ($entry->type === 'folder') {
|
||||
$path = $entry->getRawOriginal('path');
|
||||
$builder->orWhere('path', 'LIKE', "$path/%");
|
||||
}
|
||||
});
|
||||
|
||||
//only fetch children if any "where" constraints were applied
|
||||
if (count($builder->getQuery()->wheres)) {
|
||||
$children = $builder->get();
|
||||
$entries = $entries->merge($children);
|
||||
}
|
||||
|
||||
return $entries;
|
||||
}
|
||||
}
|
||||
19
common/Files/Traits/SetsAvailableSpaceAttribute.php
Executable file
19
common/Files/Traits/SetsAvailableSpaceAttribute.php
Executable file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Files\Traits;
|
||||
|
||||
trait SetsAvailableSpaceAttribute
|
||||
{
|
||||
/**
|
||||
* Large numbers are not stored in db on some servers properly without this.
|
||||
*
|
||||
* @param int $value
|
||||
*/
|
||||
public function setAvailableSpaceAttribute($value) {
|
||||
if ( ! config('database.mysql.strict') && ! is_null($value)) {
|
||||
$this->attributes['available_space'] = (string) $value;
|
||||
} else {
|
||||
$this->attributes['available_space'] = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user