Changeset View
Changeset View
Standalone View
Standalone View
src/app/Backends/Storage.php
Show All 19 Lines | class Storage | ||||
* Delete a file. | * Delete a file. | ||||
* | * | ||||
* @param \App\Fs\Item $file File object | * @param \App\Fs\Item $file File object | ||||
* | * | ||||
* @throws \Exception | * @throws \Exception | ||||
*/ | */ | ||||
public static function fileDelete(Item $file): void | public static function fileDelete(Item $file): void | ||||
{ | { | ||||
$disk = LaravelStorage::disk('files'); | $disk = LaravelStorage::disk(\config('filesystems.default')); | ||||
$path = $file->path . '/' . $file->id; | $path = $file->path . '/' . $file->id; | ||||
// TODO: Deleting files might be slow, consider marking as deleted and async job | // TODO: Deleting files might be slow, consider marking as deleted and async job | ||||
$disk->deleteDirectory($path); | $disk->deleteDirectory($path); | ||||
$file->forceDelete(); | $file->forceDelete(); | ||||
} | } | ||||
/** | /** | ||||
* Delete a file chunk. | * Delete a file chunk. | ||||
* | * | ||||
* @param \App\Fs\Chunk $chunk File chunk object | * @param \App\Fs\Chunk $chunk File chunk object | ||||
* | * | ||||
* @throws \Exception | * @throws \Exception | ||||
*/ | */ | ||||
public static function fileChunkDelete(Chunk $chunk): void | public static function fileChunkDelete(Chunk $chunk): void | ||||
{ | { | ||||
$disk = LaravelStorage::disk('files'); | $disk = LaravelStorage::disk(\config('filesystems.default')); | ||||
$path = self::chunkLocation($chunk->chunk_id, $chunk->item); | $path = self::chunkLocation($chunk->chunk_id, $chunk->item); | ||||
$disk->delete($path); | $disk->delete($path); | ||||
$chunk->forceDelete(); | $chunk->forceDelete(); | ||||
} | } | ||||
Show All 17 Lines | public static function fileDownload(Item $file): StreamedResponse | ||||
$response->headers->replace([ | $response->headers->replace([ | ||||
'Content-Type' => $props['mimetype'], | 'Content-Type' => $props['mimetype'], | ||||
'Content-Disposition' => $disposition, | 'Content-Disposition' => $disposition, | ||||
]); | ]); | ||||
$response->setCallback(function () use ($file) { | $response->setCallback(function () use ($file) { | ||||
$file->chunks()->orderBy('sequence')->get()->each(function ($chunk) use ($file) { | $file->chunks()->orderBy('sequence')->get()->each(function ($chunk) use ($file) { | ||||
$disk = LaravelStorage::disk('files'); | $disk = LaravelStorage::disk(\config('filesystems.default')); | ||||
$path = Storage::chunkLocation($chunk->chunk_id, $file); | $path = Storage::chunkLocation($chunk->chunk_id, $file); | ||||
$stream = $disk->readStream($path); | $stream = $disk->readStream($path); | ||||
fpassthru($stream); | fpassthru($stream); | ||||
fclose($stream); | fclose($stream); | ||||
}); | }); | ||||
}); | }); | ||||
Show All 12 Lines | class Storage | ||||
* @throws \Exception | * @throws \Exception | ||||
*/ | */ | ||||
public static function fileInput($stream, array $params, Item $file = null): array | public static function fileInput($stream, array $params, Item $file = null): array | ||||
{ | { | ||||
if (!empty($params['uploadId'])) { | if (!empty($params['uploadId'])) { | ||||
return self::fileInputResumable($stream, $params, $file); | return self::fileInputResumable($stream, $params, $file); | ||||
} | } | ||||
$disk = LaravelStorage::disk('files'); | $disk = LaravelStorage::disk(\config('filesystems.default')); | ||||
$chunkId = \App\Utils::uuidStr(); | $chunkId = \App\Utils::uuidStr(); | ||||
$path = self::chunkLocation($chunkId, $file); | $path = self::chunkLocation($chunkId, $file); | ||||
$disk->writeStream($path, $stream); | $disk->writeStream($path, $stream); | ||||
$fileSize = $disk->size($path); | $fileSize = $disk->size($path); | ||||
▲ Show 20 Lines • Show All 72 Lines • ▼ Show 20 Lines | protected static function fileInputResumable($stream, array $params, Item $file = null): array | ||||
$from = $params['from'] ?? 0; | $from = $params['from'] ?? 0; | ||||
// Sanity checks on the input parameters | // Sanity checks on the input parameters | ||||
// TODO: Support uploading again a chunk that already has been uploaded? | // TODO: Support uploading again a chunk that already has been uploaded? | ||||
if ($from < $upload['uploaded'] || $from > $upload['uploaded'] || $from > $upload['size']) { | if ($from < $upload['uploaded'] || $from > $upload['uploaded'] || $from > $upload['size']) { | ||||
throw new \Exception("Invalid 'from' parameter for resumable file upload."); | throw new \Exception("Invalid 'from' parameter for resumable file upload."); | ||||
} | } | ||||
$disk = LaravelStorage::disk('files'); | |||||
$disk = LaravelStorage::disk(\config('filesystems.default')); | |||||
$chunkId = \App\Utils::uuidStr(); | $chunkId = \App\Utils::uuidStr(); | ||||
$path = self::chunkLocation($chunkId, $file); | $path = self::chunkLocation($chunkId, $file); | ||||
// Save the file chunk | // Save the file chunk | ||||
$disk->writeStream($path, $stream); | $disk->writeStream($path, $stream); | ||||
// Detect file type using the first chunk | // Detect file type using the first chunk | ||||
▲ Show 20 Lines • Show All 51 Lines • ▼ Show 20 Lines | class Storage | ||||
* Get the file mime type. | * Get the file mime type. | ||||
* | * | ||||
* @param string $path File location | * @param string $path File location | ||||
* | * | ||||
* @return string File mime type | * @return string File mime type | ||||
*/ | */ | ||||
protected static function mimetype(string $path): string | protected static function mimetype(string $path): string | ||||
{ | { | ||||
$disk = LaravelStorage::disk('files'); | $disk = LaravelStorage::disk(\config('filesystems.default')); | ||||
$mimetype = $disk->mimeType($path); | $mimetype = $disk->mimeType($path); | ||||
// The mimetype may contain e.g. "; charset=UTF-8", remove this | // The mimetype may contain e.g. "; charset=UTF-8", remove this | ||||
if ($mimetype) { | if ($mimetype) { | ||||
return explode(';', $mimetype)[0]; | return explode(';', $mimetype)[0]; | ||||
} | } | ||||
Show All 16 Lines |