app.post('/api/upload-gif', upload.single('gif'), async (req, res) => { try { const gifBuffer = req.file.buffer; const gifFileName = req.file.originalname; const s3 = new aws.S3({ region: 'your-region' }); const params = { Bucket: 'your-bucket-name', Key: gifFileName, Body: gifBuffer, }; const data = await s3.upload(params).promise(); const gifUrl = data.Location;
<script> const form = document.getElementById('gif-form'); form.addEventListener('submit', async (e) => { e.preventDefault(); const fileInput = document.getElementById('gif-file'); const file = fileInput.files[0]; const formData = new FormData(); formData.append('gif', file);
// server.js const express = require('express'); const multer = require('multer'); const aws = require('aws-sdk'); const db = require('./db'); // assume a database connection
db.connect((err) => { if (err) { console.error('error connecting:', err); return; } console.log('connected as id ' + db.threadId); }); how to convert gif to url link
try { const response = await fetch('/api/upload-gif', { method: 'POST', body: formData, }); const gifUrl = await response.json(); document.getElementById('gif-url').innerHTML = `GIF URL: <a href="${gifUrl}">${gifUrl}</a>`; } catch (error) { console.error(error); } }); </script>
// db.js const mysql = require('mysql');
Here's a high-level example using Node.js, Express.js, and AWS S3: // Save GIF metadata to database await db
const db = mysql.createConnection({ host: 'your-host', user: 'your-user', password: 'your-password', database: 'your-database', });
const saveGifMetadata = async (fileName, gifUrl) => { const query = 'INSERT INTO gif_metadata (file_name, url) VALUES (?, ?)'; const values = [fileName, gifUrl]; await db.query(query, values); };
<!-- index.html --> <form id="gif-form"> <input type="file" id="gif-file" accept=".gif"> <button type="submit">Upload GIF</button> <div id="gif-url"></div> </form> } catch (error) { console.error(error)
module.exports = { saveGifMetadata }; This is a basic example to demonstrate the feature. You'll need to modify and extend it to fit your specific requirements. Additionally, ensure you handle errors, implement security measures (e.g., validate user input), and optimize performance.
// Save GIF metadata to database await db.saveGifMetadata(gifFileName, gifUrl);
res.json(gifUrl); } catch (error) { console.error(error); res.status(500).json({ error: 'Failed to upload GIF' }); } });
const app = express(); const upload = multer({ dest: './uploads/' });