Merge pull request #277 from christianbundy/add-profile-image-upload

Add profile image upload
This commit is contained in:
Cinnamon 2020-03-11 17:45:09 -07:00 committed by GitHub
commit 54e95d1071
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 53 additions and 9 deletions

View File

@ -23,6 +23,7 @@
"libtool",
"manyverse",
"mebibyte",
"mebibytes",
"minlength",
"mkdir",
"monokai",

View File

@ -356,13 +356,15 @@ router
description
});
})
.post("/profile/edit", koaBody(), async ctx => {
.post("/profile/edit", koaBody({ multipart: true }), async ctx => {
const name = String(ctx.request.body.name);
const description = String(ctx.request.body.description);
const image = await fs.promises.readFile(ctx.request.files.image.path);
ctx.body = await post.publishProfileEdit({
name,
description
description,
image
});
ctx.redirect("/profile");
})

View File

@ -8,6 +8,7 @@ const pullParallelMap = require("pull-paramap");
const pull = require("pull-stream");
const pullSort = require("pull-sort");
const ssbRef = require("ssb-ref");
const crypto = require("crypto");
// HACK: https://github.com/ssbc/ssb-thread-schema/issues/4
const isNestedReply = require("ssb-thread-schema/post/nested-reply/validator");
@ -1249,12 +1250,47 @@ module.exports = ({ cooler, isPublic }) => {
debug("Published: %O", body);
return ssb.publish(body);
},
publishProfileEdit: async ({ name, description }) => {
publishProfileEdit: async ({ name, description, image }) => {
const ssb = await cooler.open();
const body = { type: "about", about: ssb.id, name, description };
if (image.length > 0) {
// 5 MiB check
const mebibyte = Math.pow(2, 20);
const maxSize = 5 * mebibyte;
if (image.length > maxSize) {
throw new Error("Image file is too big, maximum size is 5 mebibytes");
}
const algorithm = "sha256";
const hash = crypto
.createHash(algorithm)
.update(image)
.digest("base64");
debug("Published: %O", body);
return ssb.publish(body);
const blobId = `&${hash}.${algorithm}`;
return new Promise((resolve, reject) => {
pull(
pull.values([image]),
ssb.blobs.add(blobId, err => {
if (err) {
reject(err);
} else {
const body = {
type: "about",
about: ssb.id,
name,
description,
image: blobId
};
debug("Published: %O", body);
resolve(ssb.publish(body));
}
})
);
});
} else {
const body = { type: "about", about: ssb.id, name, description };
debug("Published: %O", body);
return ssb.publish(body);
}
},
publishCustom: async options => {
const ssb = await cooler.open();

View File

@ -478,11 +478,16 @@ exports.editProfileView = ({ name, description }) =>
h1(i18n.editProfile),
p(i18n.editProfileDescription),
form(
{ action: "/profile/edit", method: "POST" },
{
action: "/profile/edit",
method: "POST",
enctype: "multipart/form-data"
},
label(
i18n.profileName,
input({ name: "name", autofocus: true, value: name })
i18n.profileImage,
input({ type: "file", name: "image", accept: "image/*" })
),
label(i18n.profileName, input({ name: "name", value: name })),
label(
i18n.profileDescription,
textarea(