Fix automatic content length workaround switch

This commit is contained in:
Cadence Ember
2025-02-28 23:30:22 +13:00
parent a8670323a0
commit f7ba176a7e
2 changed files with 30 additions and 27 deletions

View File

@@ -1,6 +1,9 @@
// @ts-check
const passthrough = require("../passthrough")
const {reg, writeRegistration} = require("./read-registration.js")
const Ty = require("../types")
const {sync, db, select} = passthrough
/** @type {import("./mreq")} */
const mreq = sync.require("./mreq")
@@ -44,11 +47,8 @@ async function uploadDiscordFileToMxc(path) {
return existingFromDb
}
// Download from Discord
const promise = fetch(url, {}).then(async res => {
// Upload to Matrix
const root = await module.exports._actuallyUploadDiscordFileToMxc(urlNoExpiry, res)
// Download from Discord and upload to Matrix
const promise = module.exports._actuallyUploadDiscordFileToMxc(url).then(root => {
// Store relationship in database
db.prepare("INSERT INTO file (discord_url, mxc_url) VALUES (?, ?)").run(urlNoExpiry, root.content_uri)
inflight.delete(urlNoExpiry)
@@ -62,17 +62,31 @@ async function uploadDiscordFileToMxc(path) {
/**
* @param {string} url
* @param {Response} res
* @returns {Promise<Ty.R.FileUploaded>}
*/
async function _actuallyUploadDiscordFileToMxc(url, res) {
const body = res.body
/** @type {import("../types").R.FileUploaded} */
const root = await mreq.mreq("POST", "/media/v3/upload", body, {
headers: {
"Content-Type": res.headers.get("content-type")
async function _actuallyUploadDiscordFileToMxc(url) {
const res = await fetch(url, {})
try {
/** @type {Ty.R.FileUploaded} */
const root = await mreq.mreq("POST", "/media/v3/upload", res.body, {
headers: {
"Content-Type": res.headers.get("content-type")
}
})
return root
} catch (e) {
if (e instanceof mreq.MatrixServerError && e.data.error?.includes("Content-Length") && !reg.ooye.content_length_workaround) {
reg.ooye.content_length_workaround = true
const root = await _actuallyUploadDiscordFileToMxc(url)
console.error("OOYE cannot stream uploads to Synapse. The `content_length_workaround` option"
+ "\nhas been activated in registration.yaml, which works around the problem, but"
+ "\nhalves the speed of bridging d->m files. A better way to resolve this problem"
+ "\nis to run an nginx reverse proxy to Synapse and re-run OOYE setup.")
writeRegistration(reg)
return root
}
})
return root
throw e
}
}
function guildIcon(guild) {

View File

@@ -5,8 +5,7 @@ const streamWeb = require("stream/web")
const {buffer} = require("stream/consumers")
const mixin = require("@cloudrac3r/mixin-deep")
const {reg, writeRegistration} = require("./read-registration.js")
const {reg} = require("./read-registration.js")
const baseUrl = `${reg.ooye.server_origin}/_matrix`
class MatrixServerError extends Error {
@@ -56,21 +55,11 @@ async function mreq(method, url, bodyIn, extra = {}) {
},
...(body && {duplex: "half"}), // https://github.com/octokit/request.js/pull/571/files
}, extra)
// console.log(baseUrl + url, opts)
const res = await fetch(baseUrl + url, opts)
const root = await res.json()
if (!res.ok || root.errcode) {
if (root.error?.includes("Content-Length") && !reg.ooye.content_length_workaround) {
reg.ooye.content_length_workaround = true
const root = await mreq(method, url, body, extra)
console.error("OOYE cannot stream uploads to Synapse. The `content_length_workaround` option"
+ "\nhas been activated in registration.yaml, which works around the problem, but"
+ "\nhalves the speed of bridging d->m files. A better way to resolve this problem"
+ "\nis to run an nginx reverse proxy to Synapse and re-run OOYE setup.")
writeRegistration(reg)
return root
}
delete opts.headers?.["Authorization"]
throw new MatrixServerError(root, {baseUrl, url, ...opts})
}