Skip to content

Commit

Permalink
Merge pull request #277 from christianbundy/add-profile-image-upload
Browse files Browse the repository at this point in the history
Add profile image upload
  • Loading branch information
cinnamon-bun authored Mar 12, 2020
2 parents a6c6f8e + dba544c commit 54e95d1
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 10 deletions.
1 change: 1 addition & 0 deletions .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"libtool",
"manyverse",
"mebibyte",
"mebibytes",
"minlength",
"mkdir",
"monokai",
Expand Down
6 changes: 4 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
})
Expand Down
46 changes: 41 additions & 5 deletions src/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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 };

debug("Published: %O", body);
return ssb.publish(body);
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");

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();
Expand Down
11 changes: 8 additions & 3 deletions src/views/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 54e95d1

Please sign in to comment.