Ezequield commited on
Commit
f238d4e
·
verified ·
1 Parent(s): 81dc2b9

Upload pages/api/process.js with huggingface_hub

Browse files
Files changed (1) hide show
  1. pages/api/process.js +101 -0
pages/api/process.js ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import axios from 'axios';
2
+ import * as cheerio from 'cheerio';
3
+ import AdmZip from 'adm-zip';
4
+
5
+ export default async function handler(req, res) {
6
+ if (req.method !== 'POST') {
7
+ return res.status(405).json({ error: 'Method not allowed' });
8
+ }
9
+
10
+ const { url } = req.body;
11
+
12
+ if (!url) {
13
+ return res.status(400).json({ error: 'URL is required' });
14
+ }
15
+
16
+ try {
17
+ // 1. Fetch the DeviantArt page
18
+ // Note: In production, you might need to handle headers or proxies to mimic a browser
19
+ const response = await axios.get(url, {
20
+ headers: {
21
+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
22
+ }
23
+ });
24
+
25
+ const html = response.data;
26
+ const $ = cheerio.load(html);
27
+
28
+ // 2. Extract image URL
29
+ // DeviantArt structures change frequently. This attempts to find the main image.
30
+ let imageUrl = '';
31
+
32
+ // Try to find the image in the standard embed structure
33
+ const imgElement = $('.dev-view-deviation img').first();
34
+ if (imgElement.length && imgElement.attr('src')) {
35
+ imageUrl = imgElement.attr('src');
36
+ } else {
37
+ // Fallback: Look for any large image in the content
38
+ const fallbackImg = $('img[src*="deviantart.net"]').filter((i, el) => {
39
+ const src = $(el).attr('src');
40
+ return src && !src.includes('avatar') && !src.includes('symbol');
41
+ }).first();
42
+
43
+ if (fallbackImg.length) {
44
+ imageUrl = fallbackImg.attr('src');
45
+ }
46
+ }
47
+
48
+ if (!imageUrl) {
49
+ return res.status(404).json({ error: 'Could not find an image on the provided page. Ensure it is a direct artwork link.' });
50
+ }
51
+
52
+ // Clean up URL (remove query parameters for better fetching)
53
+ const cleanImageUrl = imageUrl.split('?')[0];
54
+
55
+ // 3. Download the image
56
+ const imageResponse = await axios.get(cleanImageUrl, {
57
+ responseType: 'arraybuffer',
58
+ headers: {
59
+ 'Referer': url,
60
+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
61
+ }
62
+ });
63
+
64
+ const imageBuffer = Buffer.from(imageResponse.data);
65
+
66
+ // 4. Create ZIP file
67
+ const zip = new AdmZip();
68
+ const extension = cleanImageUrl.split('.').pop().split('?')[0] || 'jpg';
69
+ const fileName = `deviantart_image.${extension}`;
70
+
71
+ zip.addFile(fileName, imageBuffer);
72
+
73
+ // 5. Convert ZIP to buffer
74
+ const zipBuffer = zip.toBuffer();
75
+
76
+ // 6. Mock MediaFire Upload Logic
77
+ // Note: Actual MediaFire API requires API keys and OAuth tokens which cannot be securely embedded here.
78
+ // For the purpose of this functional demo, we serve the file directly from this endpoint.
79
+ // In a real scenario, you would send 'zipBuffer' to MediaFire's upload API here.
80
+
81
+ // Set headers to trigger browser download
82
+ res.setHeader('Content-Type', 'application/zip');
83
+ res.setHeader('Content-Disposition', `attachment; filename="deviantart_download.zip"`);
84
+ res.setHeader('Content-Length', zipBuffer.length);
85
+
86
+ return res.send(zipBuffer);
87
+
88
+ } catch (error) {
89
+ console.error('Processing error:', error.message);
90
+
91
+ if (error.response) {
92
+ return res.status(error.response.status).json({
93
+ error: `Failed to fetch DeviantArt page: ${error.response.status}`
94
+ });
95
+ }
96
+
97
+ return res.status(500).json({
98
+ error: 'An error occurred while processing the image. The link might be invalid or protected.'
99
+ });
100
+ }
101
+ }