Merge pull request #277 from christianbundy/add-profile-image-upload
Add profile image upload
This commit is contained in:
commit
54e95d1071
|
@ -23,6 +23,7 @@
|
|||
"libtool",
|
||||
"manyverse",
|
||||
"mebibyte",
|
||||
"mebibytes",
|
||||
"minlength",
|
||||
"mkdir",
|
||||
"monokai",
|
||||
|
|
|
@ -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");
|
||||
})
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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(
|
||||
|
|
Loading…
Reference in New Issue