2020-04-16 10:41:25 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-04-16 10:41:25 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2024-04-12 06:15:41 +02:00
|
|
|
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-04-16 10:41:25 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-04-16 10:41:25 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Utils;
|
|
|
|
|
2023-12-16 16:27:40 +01:00
|
|
|
use Illuminate\Http\File;
|
|
|
|
use Illuminate\Http\UploadedFile;
|
|
|
|
use Illuminate\Support\Arr;
|
|
|
|
|
2020-04-16 10:41:25 +02:00
|
|
|
class TempFile
|
|
|
|
{
|
2023-12-16 16:27:40 +01:00
|
|
|
public static function path($url): string
|
2020-09-06 11:38:10 +02:00
|
|
|
{
|
2023-12-16 16:27:40 +01:00
|
|
|
$temp_path = @tempnam(sys_get_temp_dir() . '/' . sha1(time()), basename($url));
|
2020-09-06 11:38:10 +02:00
|
|
|
copy($url, $temp_path);
|
2020-04-16 10:41:25 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
return $temp_path;
|
|
|
|
}
|
2021-02-11 04:43:48 +01:00
|
|
|
|
|
|
|
/* Downloads a file to temp storage and returns the path - used for mailers */
|
2023-12-16 16:27:40 +01:00
|
|
|
public static function filePath($data, $filename): string
|
2021-02-11 04:43:48 +01:00
|
|
|
{
|
2023-12-16 16:27:40 +01:00
|
|
|
$dir_hash = sys_get_temp_dir() . '/' . sha1(microtime());
|
2021-02-11 09:04:40 +01:00
|
|
|
|
|
|
|
mkdir($dir_hash);
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2023-12-16 16:27:40 +01:00
|
|
|
$file_path = $dir_hash . '/' . $filename;
|
2021-02-11 04:43:48 +01:00
|
|
|
|
|
|
|
file_put_contents($file_path, $data);
|
|
|
|
|
|
|
|
return $file_path;
|
|
|
|
}
|
2023-12-16 16:27:40 +01:00
|
|
|
|
|
|
|
/* create a tmp file from a base64 string: https://gist.github.com/waska14/8b3bcebfad1f86f7fcd3b82927576e38*/
|
2024-04-03 07:57:23 +02:00
|
|
|
public static function UploadedFileFromBase64(string $base64File, string|null $fileName = null, string|null $mimeType = null): UploadedFile
|
2023-12-16 16:27:40 +01:00
|
|
|
{
|
|
|
|
// Get file data base64 string
|
|
|
|
$fileData = base64_decode(Arr::last(explode(',', $base64File)));
|
|
|
|
|
|
|
|
// Create temp file and get its absolute path
|
|
|
|
$tempFile = tmpfile();
|
|
|
|
$tempFilePath = stream_get_meta_data($tempFile)['uri'];
|
|
|
|
|
|
|
|
// Save file data in file
|
|
|
|
file_put_contents($tempFilePath, $fileData);
|
|
|
|
|
|
|
|
$tempFileObject = new File($tempFilePath);
|
|
|
|
$file = new UploadedFile(
|
|
|
|
$tempFileObject->getPathname(),
|
2024-04-03 07:57:23 +02:00
|
|
|
$fileName ?: $tempFileObject->getFilename(),
|
|
|
|
$mimeType ?: $tempFileObject->getMimeType(),
|
2023-12-16 16:27:40 +01:00
|
|
|
0,
|
|
|
|
true // Mark it as test, since the file isn't from real HTTP POST.
|
|
|
|
);
|
|
|
|
|
|
|
|
// Close this file after response is sent.
|
|
|
|
// Closing the file will cause to remove it from temp director!
|
|
|
|
app()->terminating(function () use ($tempFile) {
|
|
|
|
fclose($tempFile);
|
|
|
|
});
|
|
|
|
|
|
|
|
// return UploadedFile object
|
|
|
|
return $file;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create a tmp file from a raw string: https://gist.github.com/waska14/8b3bcebfad1f86f7fcd3b82927576e38*/
|
|
|
|
public static function UploadedFileFromRaw(string $fileData, string|null $fileName = null, string|null $mimeType = null): UploadedFile
|
|
|
|
{
|
|
|
|
// Create temp file and get its absolute path
|
|
|
|
$tempFile = tmpfile();
|
|
|
|
$tempFilePath = stream_get_meta_data($tempFile)['uri'];
|
|
|
|
|
|
|
|
// Save file data in file
|
|
|
|
file_put_contents($tempFilePath, $fileData);
|
|
|
|
|
|
|
|
$tempFileObject = new File($tempFilePath);
|
|
|
|
$file = new UploadedFile(
|
|
|
|
$tempFileObject->getPathname(),
|
|
|
|
$fileName ?: $tempFileObject->getFilename(),
|
|
|
|
$mimeType ?: $tempFileObject->getMimeType(),
|
|
|
|
0,
|
|
|
|
true // Mark it as test, since the file isn't from real HTTP POST.
|
|
|
|
);
|
|
|
|
|
|
|
|
// Close this file after response is sent.
|
|
|
|
// Closing the file will cause to remove it from temp director!
|
|
|
|
app()->terminating(function () use ($tempFile) {
|
|
|
|
fclose($tempFile);
|
|
|
|
});
|
|
|
|
|
|
|
|
// return UploadedFile object
|
|
|
|
return $file;
|
|
|
|
}
|
2024-03-18 08:04:54 +01:00
|
|
|
|
|
|
|
/* create a tmp file from a raw string: https://gist.github.com/waska14/8b3bcebfad1f86f7fcd3b82927576e38*/
|
|
|
|
public static function UploadedFileFromUrl(string $url, string|null $fileName = null, string|null $mimeType = null): UploadedFile
|
|
|
|
{
|
|
|
|
// Create temp file and get its absolute path
|
|
|
|
$tempFile = tmpfile();
|
|
|
|
$tempFilePath = stream_get_meta_data($tempFile)['uri'];
|
|
|
|
|
|
|
|
// Save file data in file
|
|
|
|
file_put_contents($tempFilePath, file_get_contents($url));
|
|
|
|
|
|
|
|
$tempFileObject = new File($tempFilePath);
|
|
|
|
$file = new UploadedFile(
|
|
|
|
$tempFileObject->getPathname(),
|
|
|
|
$fileName ?: $tempFileObject->getFilename(),
|
|
|
|
$mimeType ?: $tempFileObject->getMimeType(),
|
|
|
|
0,
|
|
|
|
true // Mark it as test, since the file isn't from real HTTP POST.
|
|
|
|
);
|
|
|
|
|
|
|
|
// Close this file after response is sent.
|
|
|
|
// Closing the file will cause to remove it from temp director!
|
|
|
|
app()->terminating(function () use ($tempFile) {
|
|
|
|
fclose($tempFile);
|
|
|
|
});
|
|
|
|
|
|
|
|
// return UploadedFile object
|
|
|
|
return $file;
|
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
}
|