first commit
Some checks failed
Build / run (push) Has been cancelled

This commit is contained in:
maher
2025-10-29 11:42:25 +01:00
commit 703f50a09d
4595 changed files with 385164 additions and 0 deletions

View 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;
}
}
}

View 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);
}
}

View 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];
}
}

View 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;
}
}

View 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;
}
}
}