2018-03-06 23:56:01 +00:00
|
|
|
|
/* eslint-env node */
|
2018-07-19 03:00:10 +00:00
|
|
|
|
/* global window */
|
2018-03-06 23:56:01 +00:00
|
|
|
|
|
|
|
|
|
const FormData = require('form-data');
|
|
|
|
|
const got = require('got');
|
|
|
|
|
|
|
|
|
|
const BASE_URL = 'https://debuglogs.org';
|
2018-07-19 03:00:10 +00:00
|
|
|
|
const VERSION = window.getVersion();
|
|
|
|
|
const USER_AGENT = `Signal Desktop ${VERSION}`;
|
2018-03-06 23:56:01 +00:00
|
|
|
|
|
|
|
|
|
// Workaround: Submitting `FormData` using native `FormData::submit` procedure
|
|
|
|
|
// as integration with `got` results in S3 error saying we haven’t set the
|
|
|
|
|
// `Content-Length` header:
|
2018-03-07 20:19:02 +00:00
|
|
|
|
// https://github.com/sindresorhus/got/pull/466
|
2018-03-06 23:56:01 +00:00
|
|
|
|
const submitFormData = (form, url) =>
|
|
|
|
|
new Promise((resolve, reject) => {
|
2018-07-19 17:21:53 +00:00
|
|
|
|
form.submit(url, (error, response) => {
|
2018-03-06 23:56:01 +00:00
|
|
|
|
if (error) {
|
|
|
|
|
return reject(error);
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-19 17:21:53 +00:00
|
|
|
|
const { statusCode } = response;
|
|
|
|
|
if (statusCode !== 204) {
|
|
|
|
|
return reject(
|
|
|
|
|
new Error(`Failed to upload to S3, got status ${statusCode}`)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-06 23:56:01 +00:00
|
|
|
|
return resolve();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// upload :: String -> Promise URL
|
2018-04-27 21:25:04 +00:00
|
|
|
|
exports.upload = async content => {
|
2018-07-19 03:00:10 +00:00
|
|
|
|
const signedForm = await got.get(BASE_URL, {
|
|
|
|
|
json: true,
|
|
|
|
|
headers: {
|
|
|
|
|
'user-agent': USER_AGENT,
|
|
|
|
|
},
|
|
|
|
|
});
|
2018-03-06 23:56:01 +00:00
|
|
|
|
const { fields, url } = signedForm.body;
|
|
|
|
|
|
|
|
|
|
const form = new FormData();
|
2018-03-07 19:50:32 +00:00
|
|
|
|
// The API expects `key` to be the first field:
|
2018-03-06 23:56:01 +00:00
|
|
|
|
form.append('key', fields.key);
|
|
|
|
|
Object.entries(fields)
|
|
|
|
|
.filter(([key]) => key !== 'key')
|
|
|
|
|
.forEach(([key, value]) => {
|
|
|
|
|
form.append(key, value);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const contentBuffer = Buffer.from(content, 'utf8');
|
|
|
|
|
const contentType = 'text/plain';
|
|
|
|
|
form.append('Content-Type', contentType);
|
|
|
|
|
form.append('file', contentBuffer, {
|
|
|
|
|
contentType,
|
2018-07-19 03:00:10 +00:00
|
|
|
|
filename: `signal-desktop-debug-log-${VERSION}.txt`,
|
2018-03-06 23:56:01 +00:00
|
|
|
|
});
|
|
|
|
|
|
2018-03-07 20:19:02 +00:00
|
|
|
|
// WORKAROUND: See comment on `submitFormData`:
|
|
|
|
|
// await got.post(url, { body: form });
|
2018-03-06 23:56:01 +00:00
|
|
|
|
await submitFormData(form, url);
|
|
|
|
|
|
|
|
|
|
return `${BASE_URL}/${fields.key}`;
|
|
|
|
|
};
|