save download.php

This commit is contained in:
Easy
2024-03-28 15:30:46 +08:00
parent de972eb5a9
commit 70dbb8b567
2 changed files with 58 additions and 55 deletions

58
download.php Normal file
View File

@@ -0,0 +1,58 @@
<?php
$markdownDir = './src'; // Markdown文件所在目录
$imagesDir = './src/images'; // 图片下载目录
// 确保图片下载目录存在
if (!is_dir($imagesDir)) {
mkdir($imagesDir, 0777, true);
}
// 使用cURL下载图片
function downloadImage($url, $filepath)
{
$ch = curl_init($url);
$fp = fopen($filepath, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_REFERER, 'https://ft07.com'); // 设置Referer头部
curl_exec($ch);
curl_close($ch);
fclose($fp);
echo "Downloaded: $filepath\n";
}
// 替换Markdown中的图片链接并下载图片
function replaceImageLinksInFile($filePath, $imagesDir)
{
$data = file_get_contents($filePath);
$regex = '/!\[.*?\]\((https:\/\/res07\.ftqq\.com\/.*?\.png)\)/';
preg_match_all($regex, $data, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
$imageUrl = $match[1];
$imageName = basename($imageUrl);
$localImagePath = $imagesDir . '/' . $imageName;
// 替换Markdown中的图片链接为本地路径
$data = str_replace($imageUrl, $localImagePath, $data);
// 下载图片
downloadImage($imageUrl, $localImagePath);
}
// 保存更新后的Markdown文件
file_put_contents($filePath, $data);
echo "Updated file: $filePath\n";
}
// 读取并处理每个Markdown文件
$files = scandir($markdownDir);
foreach ($files as $file) {
if (pathinfo($file, PATHINFO_EXTENSION) === 'md') {
$filePath = $markdownDir . '/' . $file;
replaceImageLinksInFile($filePath, $imagesDir);
}
}
echo "Done.\n";

View File

@@ -1,55 +0,0 @@
import { promises as fs } from 'fs';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';
import { fetch } from 'node-fetch';
const __dirname = dirname(fileURLToPath(import.meta.url));
const markdownDir = join(__dirname, 'src'); // Markdown文件所在目录
const imagesDir = join(__dirname, 'src/images'); // 图片下载目录
// 确保图片下载目录存在
await fs.mkdir(imagesDir, { recursive: true }).catch(console.error);
// 下载图片
async function downloadImage(url, filepath) {
const response = await fetch(url,{
headers: {
'referer': 'https://ft07.com'
}
});
const buffer = await response.buffer();
await fs.writeFile(filepath, buffer);
console.log('Downloaded: ' + filepath);
}
// 替换Markdown中的图片链接并下载图片
async function replaceImageLinksInFile(filePath) {
let data = await fs.readFile(filePath, 'utf8');
const regex = /!\[.*?\]\((https:\/\/res07\.ftqq\.com\/.*?\.png)\)/g;
let match;
while ((match = regex.exec(data)) !== null) {
const imageUrl = match[1];
const imageName = imageUrl.split('/').pop();
const localImagePath = join(imagesDir, imageName);
// 替换Markdown中的图片链接为本地路径
data = data.replace(imageUrl, localImagePath);
// 下载图片
await downloadImage(imageUrl, localImagePath);
}
// 保存更新后的Markdown文件
await fs.writeFile(filePath, data, 'utf8');
console.log('Updated file: ' + filePath);
}
// 读取并处理每个Markdown文件
const files = await fs.readdir(markdownDir);
for (const file of files) {
if (file.endsWith('.md')) {
const filePath = join(markdownDir, file);
await replaceImageLinksInFile(filePath);
}
}