filehost-meta
Version:
Fetch basic file information from download links
50 lines (39 loc) • 1.08 kB
JavaScript
const axios = require('axios')
const File = require('../classes/File')
const { proxyToAxios } = require('../utils')
exports.domains = ['drive.google.com']
exports.get = async (url, proxy) => {
if (!process.env.GOOGLE_KEY) {
throw new Error('Missing env variable "GOOGLE_KEY" for Google Drive API')
}
let fileId
if (url.includes('/file/d/')) {
fileId = url.split('/')[5]
} else if (url.includes('?id=')) {
fileId = url.split('?id=')[1].split('&')[0]
}
if (!fileId) {
throw new Error('Could not extract file ID from Google Drive URL')
}
const res = await axios({
method: 'get',
url: `https://www.googleapis.com/drive/v3/files/${fileId}`,
params: {
fields: 'name,size,createdTime,modifiedTime',
key: process.env.GOOGLE_KEY
},
...proxyToAxios(proxy)
})
if (res.status !== 200) {
throw new Error(res.statusText)
}
const { name, size, createdTime, modifiedTime } = res.data
return [
new File({
name,
size,
createdAt: createdTime,
updatedAt: modifiedTime,
})
]
}