|
1 | 1 | const fs = require('fs'); |
2 | | -const request = require('request'); |
| 2 | +const path = require('path'); |
3 | 3 |
|
4 | 4 | const ServiceSettings = require('../service-settings'); |
5 | 5 |
|
@@ -32,40 +32,41 @@ class Service extends ServiceSettings { |
32 | 32 | this.loadSettings(); |
33 | 33 | } |
34 | 34 |
|
35 | | - upload(filePath, callback) { |
| 35 | + async upload(filePath, callback) { |
36 | 36 | if (!this.getSetting('upload_url')) return callback(new Error('No upload URL configured for upload')); |
37 | 37 |
|
38 | | - request.post({ |
39 | | - url: this.getSetting('upload_url'), |
40 | | - formData: { |
41 | | - 'files[]': { |
42 | | - value: fs.createReadStream(filePath), |
43 | | - options: { |
44 | | - contentType: 'image/png' |
45 | | - } |
46 | | - } |
47 | | - } |
48 | | - }, (err, response, body) => { |
49 | | - if (err || !response || response.statusCode >= 300) { |
50 | | - return callback(new Error(`HTTP error occurred: ${err ? err.message : `${response && response.statusCode} server response code`}`)); |
51 | | - } |
52 | | - if (typeof body === 'string') { |
53 | | - try { |
54 | | - body = JSON.parse(body); |
55 | | - } catch (err) { |
56 | | - return callback(new Error('The Pomf API returned an invalid JSON response')); |
57 | | - } |
58 | | - } |
59 | | - if (!body.success || !body.files || body.files.length === 0) { |
60 | | - return callback(new Error('The Pomf API returned an unexpected response')); |
61 | | - } |
| 38 | + let response; |
| 39 | + try { |
| 40 | + const fileBuffer = fs.readFileSync(filePath); |
| 41 | + const formData = new FormData(); |
| 42 | + formData.append('files[]', new Blob([fileBuffer], { type: 'image/png' }), path.basename(filePath)); |
62 | 43 |
|
63 | | - let result_url = this.getSetting('result_url'); |
64 | | - if (result_url.length > 0 && result_url[result_url.length - 1] !== '/') { |
65 | | - result_url += '/'; |
66 | | - } |
67 | | - callback(null, result_url + body.files[0].url); |
68 | | - }); |
| 44 | + response = await fetch(this.getSetting('upload_url'), { |
| 45 | + method: 'POST', |
| 46 | + body: formData |
| 47 | + }); |
| 48 | + } catch (err) { |
| 49 | + return callback(new Error(`HTTP error occurred: ${err.message}`)); |
| 50 | + } |
| 51 | + if (response.status >= 300) { |
| 52 | + return callback(new Error(`HTTP error occurred: ${response.status} server response code`)); |
| 53 | + } |
| 54 | + |
| 55 | + let body; |
| 56 | + try { |
| 57 | + body = await response.json(); |
| 58 | + } catch (err) { |
| 59 | + return callback(new Error('The Pomf API returned an invalid JSON response')); |
| 60 | + } |
| 61 | + if (!body.success || !body.files || body.files.length === 0) { |
| 62 | + return callback(new Error('The Pomf API returned an unexpected response')); |
| 63 | + } |
| 64 | + |
| 65 | + let result_url = this.getSetting('result_url'); |
| 66 | + if (result_url.length > 0 && result_url[result_url.length - 1] !== '/') { |
| 67 | + result_url += '/'; |
| 68 | + } |
| 69 | + callback(null, result_url + body.files[0].url); |
69 | 70 | } |
70 | 71 | } |
71 | 72 |
|
|
0 commit comments