Spaces:
Build error
Build error
Create server.js
Browse files
server.js
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const express = require('express');
|
| 2 |
+
const multer = require('multer');
|
| 3 |
+
const { google } = require('googleapis');
|
| 4 |
+
const fs = require('fs');
|
| 5 |
+
const path = require('path');
|
| 6 |
+
|
| 7 |
+
const app = express();
|
| 8 |
+
|
| 9 |
+
// Konfigurasi multer untuk menangani upload file
|
| 10 |
+
const upload = multer({ dest: 'uploads/' });
|
| 11 |
+
|
| 12 |
+
// Setup Google Drive API credentials
|
| 13 |
+
const CLIENT_ID = process.env.GOOGLE_DRIVE_CLIENT_ID;
|
| 14 |
+
const CLIENT_SECRET = process.env.GOOGLE_DRIVE_CLIENT_SECRET;
|
| 15 |
+
const REFRESH_TOKEN = process.env.GOOGLE_DRIVE_REFRESH_TOKEN;
|
| 16 |
+
const FOLDER_ID = process.env.GOOGLE_DRIVE_FOLDER_ID;
|
| 17 |
+
|
| 18 |
+
const oauth2Client = new google.auth.OAuth2(
|
| 19 |
+
CLIENT_ID,
|
| 20 |
+
CLIENT_SECRET,
|
| 21 |
+
"https://developers.google.com/oauthplayground" // Redirect URL (tidak digunakan)
|
| 22 |
+
);
|
| 23 |
+
|
| 24 |
+
oauth2Client.setCredentials({
|
| 25 |
+
refresh_token: REFRESH_TOKEN,
|
| 26 |
+
});
|
| 27 |
+
|
| 28 |
+
const drive = google.drive({
|
| 29 |
+
version: 'v3',
|
| 30 |
+
auth: oauth2Client,
|
| 31 |
+
});
|
| 32 |
+
|
| 33 |
+
// Function to upload file to Google Drive
|
| 34 |
+
async function uploadFileToGoogleDrive(file) {
|
| 35 |
+
try {
|
| 36 |
+
const fileMetadata = {
|
| 37 |
+
'name': file.originalname,
|
| 38 |
+
'parents': [FOLDER_ID], // Folder ID tempat file akan disimpan
|
| 39 |
+
};
|
| 40 |
+
|
| 41 |
+
const media = {
|
| 42 |
+
mimeType: file.mimetype,
|
| 43 |
+
body: fs.createReadStream(file.path),
|
| 44 |
+
};
|
| 45 |
+
|
| 46 |
+
const response = await drive.files.create({
|
| 47 |
+
resource: fileMetadata,
|
| 48 |
+
media: media,
|
| 49 |
+
fields: 'id',
|
| 50 |
+
});
|
| 51 |
+
|
| 52 |
+
return response.data.id;
|
| 53 |
+
} catch (error) {
|
| 54 |
+
throw new Error('Gagal mengunggah ke Google Drive: ' + error.message);
|
| 55 |
+
}
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
// Endpoint untuk upload file
|
| 59 |
+
app.post('/upload', upload.single('file'), async (req, res) => {
|
| 60 |
+
try {
|
| 61 |
+
if (!req.file) {
|
| 62 |
+
return res.status(400).send('No file uploaded.');
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
const fileId = await uploadFileToGoogleDrive(req.file);
|
| 66 |
+
|
| 67 |
+
// Hapus file lokal setelah diunggah ke Google Drive
|
| 68 |
+
fs.unlinkSync(req.file.path);
|
| 69 |
+
|
| 70 |
+
res.json({
|
| 71 |
+
message: 'File uploaded successfully to Google Drive!',
|
| 72 |
+
fileId: fileId,
|
| 73 |
+
});
|
| 74 |
+
} catch (error) {
|
| 75 |
+
res.status(500).send('Error uploading file: ' + error.message);
|
| 76 |
+
}
|
| 77 |
+
});
|
| 78 |
+
|
| 79 |
+
// Start server
|
| 80 |
+
const PORT = process.env.PORT || 3000;
|
| 81 |
+
app.listen(PORT, () => {
|
| 82 |
+
console.log(`Server running on port ${PORT}`);
|
| 83 |
+
});
|