Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add image insertion support to codepress #89

Merged
merged 4 commits into from
Mar 12, 2020

Conversation

iansinnott
Copy link
Member

@iansinnott iansinnott commented Mar 11, 2020

What this does:

  • Allows uploading images in the codepress editor
  • Uploaded images are stored in the public/ directory of the client project
  • Disables live reload when codepress is active

The rationale behind the second point is that we ideally want any images or
assets of any kind associated with challenges to be available as long as the
challenges are. For this reason using external URLs is less appealing than it
otherwise might be.

The alternative to this would be dropping images into a google storage bucket,
however that would require codepress users to be authorized with gcloud which is
a whole nother can of worms that I'd rather avoid for now.


NOTE: There are a few issues with the implementation...

  • Rich markdown editor creates invalid markup (p > div) and React yells at us
    • This is not a significant issue as far as I can tell, browsers were designed to handle invalid markup and having a div inside a p tag seems fine.
    • I didn't find a way to customize the rendering of the images
  • The editor might blow-up after inserting an image and immediately leaving edit mode

This second point sucks, and I'm not sure what's going on there however my guess
is that rich-markdown-editor is rendering the image in such a way that the key
is prone to change often. I saw this in the inspector, although I'm not sure it's
the issue.

However, I'd still like to ship this even with caveats because we do not upload
many images and even when the editor does break the output markdown is correct
and it is read back correctly meaning it is not a user facing bug from what I
can tell.

@iansinnott iansinnott requested a review from bonham000 March 11, 2020 15:39
Copy link
Contributor

@bonham000 bonham000 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! 💯

A few comments.

const filename = `${file.md5}_${file.name}`;
const filepath = path.join(absDir, filename);

console.log(`[UPLOAD] saving file for ${req.params.resourceId} at `);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this supposed to log the filename, e.g. at ...? ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah... Yes it is 😅

console.log(`[UPLOAD] saving file for ${req.params.resourceId} at `);
file.mv(filepath, err => {
if (err) {
next(err);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this still result in res.send getting called later on line 142?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh good call. I don't think so, but even if not it's not obvious so I'll add an early return

packages/client/public/index.html Show resolved Hide resolved
@@ -294,6 +313,13 @@ class ContentEditor extends React.Component<Props> {
.toPromise();
};

handleFileUpload = (file: File): Promise<string> => {
if (!this.props.challengeId) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to move the error checking to the uploadFile method?

Not a big deal if it stays here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that would work too. Will update.

@iansinnott iansinnott merged commit 7765b47 into master Mar 12, 2020
@iansinnott iansinnott deleted the feat/codepress-image-upload branch March 12, 2020 12:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants