How to read a file from a remote disk ? #142
-
Hello, Can you please let me know if there is an option to read a file from another file system except for local ? Actually, is it possible to define in Thanks a lot |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Reading from other filesystems is out of scope for this package. |
Beta Was this translation helpful? Give feedback.
-
Really enjoy your work, @freekmurze! I've developed a wrapper for the In the event a file is downloaded, I've implemented a shutdown cleanup process that automatically removes the temporary file after use. The wrapper also supports handling temporary files created by Livewire. While there may be better solutions, this solution has been performing effectively for me. Example: $headers = SimpleExcelReaderWrapper::create(
Storage::($import->disk)->readStream($import->file)
)->getHeaders(); the wrapper namespace App\Helpers\Excel;
use Illuminate\Support\Str;
use Livewire\Features\SupportFileUploads\TemporaryUploadedFile;
use Spatie\SimpleExcel\SimpleExcelReader;
class SimpleExcelReaderWrapper extends SimpleExcelReader
{
public static array $csvTypes = ['csv'];
public static array $xlsxTypes = ['xlsx', 'excel', 'spreadsheet'];
public static bool $shouldCleanupTempFiles = true;
private static string $tempFilePath;
private static mixed $inputFile;
private static string $inputFileType;
public static function create(mixed $file, string $type = ''): static
{
static::setInputFileType($type);
static::setInputFile(
static::getLocalFilePath($file)
);
return new static(
static::getInputFile(),
static::matchMimeTypeToFileType()
);
}
protected static function matchMimeTypeToFileType(?string $type = null): string
{
if (blank($type)) {
$type = static::getMimeType();
}
if (Str::contains($type, static::getCsvMatchTypes(), true)) {
return 'csv';
}
if (Str::contains($type, static::getXlsxMatchTypes(), true)) {
return 'xlsx';
}
return $type ?: '';
}
protected static function getMimeType($file = null): string
{
if (static::getInputFileType() && ! blank(static::getInputFileType())) {
return static::getInputFileType();
}
if (blank($file)) {
$file = static::getInputFile();
}
if ($filePath = static::getLocalFilePath($file)) {
return mime_content_type($filePath);
}
return '';
}
protected static function getLocalFilePath($file = null): ?string
{
if (blank($file)) {
$file = static::getInputFile();
}
return once(static function () use ($file) {
if (blank($file)) {
return null;
}
if (is_string($file)) {
return $file;
}
if ($file instanceof TemporaryUploadedFile) {
static::setInputFile($file->readStream());
$file = static::getInputFile();
}
if (! is_resource($file)) {
//ray('reader->getLocalFilePath: $file is not a resource', $file);
throw new \InvalidArgumentException('The file must be a path string or a resource');
}
$metadata = stream_get_meta_data($file);
// If the file in the stream is a local file, return the path
if ($metadata['wrapper_type'] === 'plainfile' && file_exists($metadata['uri'])) {
return $metadata['uri'];
}
if (get_resource_type($file) === 'stream') {
if (static::shouldCleanupTempFiles()) {
register_shutdown_function(static function () {
static::cleanupTempFiles();
});
}
static::setTempFilePath(tempnam(sys_get_temp_dir(), 'stream_data_'));
$tempFile = fopen(static::getTempFilePath(), 'w+b');
stream_copy_to_stream($file, $tempFile);
fclose($tempFile);
return static::getTempFilePath();
}
//ray('reader->getLocalFilePath: The resource type is not supported: '.get_resource_type($file));
throw new \InvalidArgumentException('The file resource type is not supported: '.get_resource_type($file));
});
}
public static function getTempFilePath(): string
{
return static::$tempFilePath;
}
protected static function setTempFilePath(string $path): string
{
static::$tempFilePath = $path;
return $path;
}
protected static function setInputFile(mixed $file): mixed
{
static::$inputFile = $file;
return $file;
}
protected static function getInputFile(): mixed
{
return static::$inputFile ?? null;
}
protected static function setInputFileType(string $type): string
{
static::$inputFileType = $type;
return $type;
}
protected static function getInputFileType(): string
{
return static::$inputFileType;
}
protected static function getCsvMatchTypes(): array
{
return static::$csvTypes;
}
protected static function getXlsxMatchTypes(): array
{
return static::$xlsxTypes;
}
protected static function shouldCleanupTempFiles(): bool
{
return static::$shouldCleanupTempFiles;
}
public static function cleanupTempFiles(): void
{
if (file_exists(static::getTempFilePath())) {
unlink(static::getTempFilePath());
}
}
} Cheers! Jason |
Beta Was this translation helpful? Give feedback.
Really enjoy your work, @freekmurze!
I've developed a wrapper for the
SimpleExcelReader
to enhance its functionality, enabling the handling of both local and remote files primarly for reading. This enhancement is achieved through a mechanism that detects if a file is a stream resource. When working with remote files, it downloads them to a temporary file. If the stream resource is already a local file then it just uses it without downloading.In the event a file is downloaded, I've implemented a shutdown cleanup process that automatically removes the temporary file after use.
The wrapper also supports handling temporary files created by Livewire. While there may be better solutions, this …