// # config/filesystems.php
return [
'default' => env('FILESYSTEM_DRIVER', 'local'),
'cloud' => env('FILESYSTEM_CLOUD', 's3'),
/*
|--------------------------------------------------------------------------
| Filesystem Disks
|--------------------------------------------------------------------------
|
| Here you may configure as many filesystem "disks" as you wish, and you
| may even configure multiple disks of the same driver. Defaults have
| been setup for each driver as an example of the required options.
|
| Supported Drivers: "local", "ftp", "sftp", "s3", "rackspace"
|
*/
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path(''),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
's3' => [
'driver' => 's3',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION'),
'bucket' => env('AWS_BUCKET'),
'url' => env('AWS_URL'),
'endpoint' => env('AWS_ENDPOINT'),
],
],
];
#参考withFacades 错误原因,Facades\Event 名称命名冲突
#https://blog.csdn.net/u013372487/article/details/107004972
# bootstrap/app.php
$Facades_Alias_Defaults = [
\Illuminate\Support\Facades\Auth::class => 'Auth',
\Illuminate\Support\Facades\Cache::class => 'Cache',
\Illuminate\Support\Facades\DB::class => 'DB',
\Illuminate\Support\Facades\Event::class => 'LumenEvent',
\Illuminate\Support\Facades\Gate::class => 'Gate',
\Illuminate\Support\Facades\Log::class => 'Log',
\Illuminate\Support\Facades\Queue::class => 'Queue',
\Illuminate\Support\Facades\Route::class => 'Route',
\Illuminate\Support\Facades\Schema::class => 'Schema',
\Illuminate\Support\Facades\Storage::class => 'Storage',
\Illuminate\Support\Facades\URL::class => 'URL',
\Illuminate\Support\Facades\Validator::class => 'Validator',
];
// $app->withFacades();
// $app->withFacades(true, [ \Illuminate\Support\Facades\Event::class => 'LumenEvent', ]);
$app->withFacades(true, $Facades_Alias_Defaults );
$app->withEloquent();
$app->configure('filesystems');
$app->register(Illuminate\Filesystem\FilesystemServiceProvider::class);
$app->register(App\Providers\AppServiceProvider::class);
# bootstrap/app.php
$app->withFacades();
$app->withEloquent();
$app->configure('filesystems');
$app->register(Illuminate\Filesystem\FilesystemServiceProvider::class);
$app->register(App\Providers\AppServiceProvider::class);
// 在route/web.php 中添加
// use App\Http\Controllers\BingController;
$router->get('/bing', 'BingController@bing');
$router->get('/{id}', 'BingController@index');
// # app/Http/Controllers/BingController.php
// #
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Log;
// $directory = 'your_directory_path';
// try {
// $result = Storage::disk('local')->makeDirectory($directory);
// if (!$result) {
// Log::error("Failed to create directory: $directory");
// }
// } catch (\Exception $e) {
// Log::error("Error creating directory: " . $e->getMessage());
// }
class BingController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
//
}
//
public function index($id)
{
return "这是控制器Bing的 index 方法,ID 为: $id";
}
public function bing()
{
// $localFilePath = __DIR__ . '/../storage/app/bing_wallpaper.jpg';
$bing_wallpaper_dir_path = 'app/'.DATE("Ym").'/';
// $bing_wallpaper_dir_path = 'app/';
$bing_wallpaper_filename = date("Ymd").'.jpg';
$localFilePath = storage_path($bing_wallpaper_dir_path.$bing_wallpaper_filename );
// use Illuminate\Support\Facades\Storage;
// $content = Storage::disk('local')->get('your_file.txt');
// echo $content;
if (file_exists($localFilePath)) {
$image = file_get_contents($localFilePath);
header('localFilePath: '. $localFilePath);
header('Content-Type: image/jpeg');
echo $image;
return;
}
// 检查目录是否存在,如果不存在则创建
if (!Storage::disk('local')->exists($bing_wallpaper_dir_path)) {
Storage::disk('local')->makeDirectory($bing_wallpaper_dir_path,null,true);
}
$directory = $bing_wallpaper_dir_path;
try {
$result = Storage::disk('local')->makeDirectory($directory,null,true );
if (!$result) {
Log::error("Failed to create directory: $directory");
}
} catch (\Exception $e) {
Log::error("Error creating directory: " . $e->getMessage());
}
// 下载并保存图片
$bingApiUrl = 'https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1';
$response = file_get_contents($bingApiUrl);
$data = json_decode($response, true);
if ($data && isset($data['images'][0]['url'])) {
$imageUrl = 'https://www.bing.com' . $data['images'][0]['url'];
$imageData = file_get_contents($imageUrl);
if ($imageData) {
file_put_contents($localFilePath, $imageData);
header('Content-Type: image/jpeg');
echo $imageData;
} else {
return response()->json(['error' => 'Failed to download image'], 500);
}
} else {
return response()->json(['error' => 'Failed to get Bing wallpaper URL'], 500);
}
}
}