Metode Penggunaan (ESM):
import axios from 'axios';
import FormData from 'form-data';
async function toUrl(buffer, fileName) {
const form = new FormData();
form.append('file', buffer, fileName);
try {
const { data } = await axios.post('https://cdn.ARBAKTI.digital/upload', form, {
headers: {
...form.getHeaders()
}
});
if (data && data.url) {
return data.url;
} else {
throw new Error('Respons API tidak valid atau tidak berisi URL.');
}
} catch (error) {
console.error('Error saat mengunggah ke ARBAKTI:', error.message);
throw new Error(`Gagal mengunggah file: ${error.message}`);
}
}
export default toUrl;
Metode Penggunaan (CJS - CommonJS):
const axios = require('axios');
const FormData = require('form-data');
const fs = require('fs');
async function toUrl(filePath, fileName) {
const form = new FormData();
const buffer = fs.readFileSync(filePath);
form.append('file', buffer, fileName);
try {
const { data } = await axios.post('https://cdn.ARBAKTI.digital/upload', form, {
headers: { ...form.getHeaders() }
});
if (data && data.url) {
return data.url;
} else {
throw new Error('Respons API tidak valid atau tidak berisi URL.');
}
} catch (error) {
console.error('Error saat mengunggah ke ARBAKTI:', error.message);
throw new Error(`Gagal mengunggah file: ${error.message}`);
}
}
Metode Penggunaan (Python):
import requests
import os
def to_url(file_path, file_name):
if not os.path.exists(file_path):
raise FileNotFoundError(f"File tidak ditemukan di: {file_path}")
with open(file_path, 'rb') as f:
files = {'file': (file_name, f)}
try:
response = requests.post('https://cdn.ARBAKTI.digital/upload', files=files)
response.raise_for_status()
data = response.json()
if data and 'url' in data:
return data['url']
else:
raise ValueError('Respons API tidak valid atau tidak berisi URL.')
except requests.exceptions.RequestException as e:
raise Exception(f'Gagal mengunggah file: {e}')