-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import * as fs from "fs"; | |
import morgan from "morgan"; | ||
import * as path from "path"; | ||
import { promisify } from "util"; | ||
import fileUpload from "express-fileupload"; | ||
|
||
import { Course, ContentUtilityClass } from "@pairwise/common"; | ||
|
||
|
@@ -109,6 +110,43 @@ const app = express(); | |
app.use(morgan("dev")); | ||
app.use(cors()); | ||
app.use(bodyParser.json({ limit: "10mb" })); | ||
app.use( | ||
fileUpload({ | ||
createParentPath: true, | ||
}), | ||
); | ||
|
||
app.post("/assets/:resourceId", (req, res, next) => { | ||
const { resourceId } = req.params; | ||
if (!resourceId || !resourceId.length) { | ||
const err = new Error("No resourceId found for upload request"); | ||
next(err); | ||
return; | ||
} | ||
|
||
const relDir = `assets/${req.params.resourceId}`; | ||
const absDir = path.resolve(__dirname, "../../public", relDir); | ||
// We only care about one file, and this just makes the types pass | ||
const file = Array.isArray(req.files.asset) | ||
? req.files.asset[0] | ||
: req.files.asset; | ||
const filename = `${file.md5}_${file.name}`; | ||
const filepath = path.join(absDir, filename); | ||
|
||
console.log(`[UPLOAD] saving file for ${req.params.resourceId} at `); | ||
file.mv(filepath, err => { | ||
if (err) { | ||
next(err); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would this still result in There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
} | ||
|
||
res.send({ | ||
status: "OK", | ||
data: { | ||
filepath: path.join("/", relDir, filename), | ||
}, | ||
}); | ||
}); | ||
}); | ||
|
||
app.get("/courses", (_, res) => { | ||
api.courses.getAll().then(courses => | ||
|
@@ -171,4 +209,12 @@ app.get("/", (req, res) => { | |
}); | ||
}); | ||
|
||
app.use((err, _, res, __) => { | ||
res.status(500).send({ | ||
status: "Error", | ||
data: null, | ||
error: err.message, | ||
}); | ||
}); | ||
|
||
export default app; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,9 +12,28 @@ import { map } from "rxjs/operators"; | |
import { Editor } from "slate-react"; | ||
import { Leaf, Selection } from "slate"; | ||
import { List } from "immutable"; | ||
import { CODEPRESS_HOST, CODEPRESS } from "tools/client-env"; | ||
|
||
const RichMarkdownEditor = React.lazy(() => import("rich-markdown-editor")); | ||
|
||
const uploadFile = (file: File, resourceId: string): Promise<string> => { | ||
const formData = new FormData(); | ||
|
||
formData.append("asset", file, file.name); | ||
|
||
return fetch(`${CODEPRESS_HOST}/assets/${resourceId}`, { | ||
method: "POST", | ||
body: formData, | ||
redirect: "follow", | ||
}) | ||
.then(x => x.json()) | ||
.then(x => x.data.filepath) | ||
.catch(err => { | ||
console.warn("[ERR]", err); | ||
toaster.error(`Could not upload\n${err.message}`); | ||
}); | ||
}; | ||
|
||
// All adapted from the markdown shortcuts that ship with the lib by default: | ||
// https://github.com/outline/rich-markdown-editor/blob/master/src/plugins/MarkdownShortcuts.js | ||
const MarkdownShortcuts = (): SlatePlugin => { | ||
|
@@ -294,6 +313,13 @@ class ContentEditor extends React.Component<Props> { | |
.toPromise(); | ||
}; | ||
|
||
handleFileUpload = (file: File): Promise<string> => { | ||
if (!this.props.challengeId) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it make sense to move the error checking to the Not a big deal if it stays here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah that would work too. Will update. |
||
return Promise.reject(new Error("No challenge ID provided for upload")); | ||
} | ||
return uploadFile(file, this.props.challengeId); | ||
}; | ||
|
||
render() { | ||
const { history, plugins = [], ...props } = this.props; | ||
return ( | ||
|
@@ -302,6 +328,7 @@ class ContentEditor extends React.Component<Props> { | |
plugins={[...plugins, markdownShortcuts]} | ||
theme={editorTheme} | ||
onSearchLink={this.getSearchLinks} | ||
uploadImage={this.handleFileUpload} | ||
onShowToast={message => { | ||
toaster.toast.show({ | ||
message, | ||
|
@@ -528,6 +555,7 @@ const EditorExternalStyles = styled.div` | |
|
||
const mapStateToProps = (state: ReduxStoreState) => ({ | ||
searchResults: Modules.selectors.challenges.getSearchResults(state), | ||
challengeId: Modules.selectors.challenges.getCurrentChallengeId(state), | ||
}); | ||
|
||
const dispatchProps = { | ||
|
There was a problem hiding this comment.
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 ...?
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah... Yes it is 😅