drinkuniappvue3new/download-images.js

136 lines
4.3 KiB
JavaScript

const https = require('https');
const fs = require('fs');
const path = require('path');
// Create the target directory if it doesn't exist
const targetDir = path.join(__dirname, 'static', 'images', 'mine');
if (!fs.existsSync(targetDir)) {
fs.mkdirSync(targetDir, { recursive: true });
}
// Define the images to download with their URLs and target filenames
const images = [
{
url: 'https://lanhu-oss-2537-2.lanhuapp.com/FigmaDDSSlicePNG3d7cb8a5a5c535dbdf3c899d2d323899.png',
filename: 'default.png'
},
{
url: 'https://lanhu-oss-2537-2.lanhuapp.com/FigmaDDSSlicePNG68c4f4a5f6a9cba03731ba76dfceca0e.png',
filename: 'arrow_right_small.png'
},
{
url: 'https://lanhu-oss-2537-2.lanhuapp.com/FigmaDDSSlicePNGdc763c9311112614ded8231482e23616.png',
filename: 'order_icon.png'
},
{
url: 'https://lanhu-oss-2537-2.lanhuapp.com/FigmaDDSSlicePNGf2f2ee9713d560fcb67c832e5fcee99a.png',
filename: 'arrow_right.png'
},
{
url: 'https://lanhu-oss-2537-2.lanhuapp.com/FigmaDDSSlicePNG02e5a238d57e63442f9a31863b613a63.png',
filename: 'history_icon.png'
},
{
url: 'https://lanhu-dds-backend.oss-cn-beijing.aliyuncs.com/merge_image/imgs/d5f6ba8545d04dd8b93333b07bbc9592_mergeImage.png',
filename: 'customer_service_icon.png'
},
{
url: 'https://lanhu-oss-2537-2.lanhuapp.com/FigmaDDSSlicePNG7a8d2e771bc19f2841728a7511507d77.png',
filename: 'help_center_icon.png'
},
{
url: 'https://lanhu-oss-2537-2.lanhuapp.com/FigmaDDSSlicePNG732c7c9ddf5994412cdbd02a77dac3e5.png',
filename: 'about_us_icon.png'
},
{
url: 'https://lanhu-oss-2537-2.lanhuapp.com/FigmaDDSSlicePNGb9d46e9970cf4f97f8074aeeb4f3ac5c.png',
filename: 'logout_icon.png'
},
{
url: 'https://lanhu-oss-2537-2.lanhuapp.com/FigmaDDSSlicePNGe0d56b7debf0486f50a80459dcd82228.png',
filename: 'navigation_icon.png'
},
{
url: 'https://lanhu-oss-2537-2.lanhuapp.com/FigmaDDSSlicePNG3bb0ad8451bd7dd9f96ac85130967887.png',
filename: 'background.png'
},
{
url: 'https://lanhu-oss-2537-2.lanhuapp.com/FigmaDDSSlicePNGce2485e84ba3205457f0b0e6921852b5.png',
filename: 'integral_card.png'
},
{
url: 'https://lanhu-oss-2537-2.lanhuapp.com/FigmaDDSSlicePNGa21607a7510c39025ba69fc44da86eb2.png',
filename: 'coupon_card.png'
},
// Use order icon as recruit icon
{
url: 'https://lanhu-oss-2537-2.lanhuapp.com/FigmaDDSSlicePNGdc763c9311112614ded8231482e23616.png',
filename: 'recruit_icon.png'
},
// Use a suitable icon for address from the order icon
{
url: 'https://lanhu-oss-2537-2.lanhuapp.com/FigmaDDSSlicePNGdc763c9311112614ded8231482e23616.png',
filename: 'address_icon.png'
}
];
// Function to download an image
function downloadImage(url, filename) {
return new Promise((resolve, reject) => {
const targetPath = path.join(targetDir, filename);
console.log(`Downloading ${url} to ${targetPath}...`);
// Create a write stream
const file = fs.createWriteStream(targetPath);
// Make the request to download the image
const request = https.get(url, (response) => {
// Check if the response is successful
if (response.statusCode !== 200) {
reject(new Error(`Failed to download ${url}: ${response.statusCode} ${response.statusMessage}`));
return;
}
// Pipe the response to the file
response.pipe(file);
// Handle completion of the download
file.on('finish', () => {
file.close();
console.log(`Downloaded ${filename} successfully!`);
resolve();
});
});
// Handle request errors
request.on('error', (err) => {
fs.unlink(targetPath, () => {}); // Delete the file if there's an error
reject(err);
});
// Handle file write errors
file.on('error', (err) => {
fs.unlink(targetPath, () => {}); // Delete the file if there's an error
reject(err);
});
});
}
// Download all images sequentially
async function downloadAllImages() {
console.log('Starting to download images...');
for (const image of images) {
try {
await downloadImage(image.url, image.filename);
} catch (error) {
console.error(`Error downloading ${image.filename}: ${error.message}`);
}
}
console.log('Finished downloading all images!');
}
// Execute the download
downloadAllImages();