diff --git a/src/routes/profile.js b/src/routes/profile.js index ee0b538f9..759ae5353 100644 --- a/src/routes/profile.js +++ b/src/routes/profile.js @@ -112,11 +112,12 @@ async function getAvatar(req, res, next) { async function setBackgroundImage(req, res, next) { assert.strictEqual(typeof req.user, 'object'); - console.log(req.files.backgroundImage) - if (!req.files || !req.files.backgroundImage) return next(new HttpError(400, `backgroundImage must be a file`)); + let backgroundImage = null; - const backgroundImage = safe.fs.readFileSync(req.files.backgroundImage.path); - if (!backgroundImage) return next(BoxError.toHttpError(new BoxError(BoxError.FS_ERROR, safe.error.message))); + if (req.files && req.files.backgroundImage) { + backgroundImage = safe.fs.readFileSync(req.files.backgroundImage.path); + if (!backgroundImage) return next(BoxError.toHttpError(new BoxError(BoxError.FS_ERROR, safe.error.message))); + } const [error] = await safe(users.setBackgroundImage(req.user.id, backgroundImage)); if (error) return next(BoxError.toHttpError(error)); diff --git a/src/users.js b/src/users.js index 02a77f0fa..2c8ccda77 100644 --- a/src/users.js +++ b/src/users.js @@ -917,7 +917,7 @@ async function getBackgroundImage(id) { async function setBackgroundImage(id, backgroundImage) { assert.strictEqual(typeof id, 'string'); - assert(Buffer.isBuffer(backgroundImage)); + assert(Buffer.isBuffer(backgroundImage) || backgroundImage === null); const result = await database.query('UPDATE users SET backgroundImage=? WHERE id = ?', [ backgroundImage, id ]); if (result.length === 0) throw new BoxError(BoxError.NOT_FOUND, 'User not found');