Load new math resources in the native Web editor
This commit is contained in:
1 parent
8b6dab8d52
commit
fe86163767
12 files changed
+838
-106
No files matched your search
@@ -0,0 +1,173 @@
|
||||
use super::{EditorSession, InputProfile, fs, json, prepare, projects::ProjectStore, reply};
|
||||
use std::{
|
||||
io::{ErrorKind, Read},
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
use tiny_http::Request;
|
||||
|
||||
pub(super) const SCRIPT: &str = include_str!("library.js");
|
||||
const MAX_ZIP_UPLOAD: u64 = 64 * 1024 * 1024;
|
||||
|
||||
pub(super) struct Library {
|
||||
store: ProjectStore,
|
||||
format: PathBuf,
|
||||
manifest: PathBuf,
|
||||
runs: PathBuf,
|
||||
profile: InputProfile,
|
||||
}
|
||||
|
||||
impl Library {
|
||||
pub(super) fn new(
|
||||
store: PathBuf,
|
||||
format: PathBuf,
|
||||
manifest: PathBuf,
|
||||
runs: PathBuf,
|
||||
profile: InputProfile,
|
||||
) -> Result<Self, Box<dyn std::error::Error>> {
|
||||
fs::create_dir_all(&store)?;
|
||||
let store = store.canonicalize()?;
|
||||
fs::create_dir_all(&runs)?;
|
||||
let runs = runs.canonicalize()?;
|
||||
if runs.starts_with(&store) || store.starts_with(&runs) {
|
||||
return Err("Project and compilation directories must be separate".into());
|
||||
}
|
||||
Ok(Self {
|
||||
store: ProjectStore::new(store)?,
|
||||
format,
|
||||
manifest,
|
||||
runs,
|
||||
profile,
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn list(&self, request: Request) {
|
||||
match self.store.list() {
|
||||
Ok(projects) => reply(request, 200, json!({"projects": projects})),
|
||||
Err(error) => reply(request, 500, json!({"error": error})),
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn import(&self, mut request: Request) {
|
||||
if !request.headers().iter().any(|header| {
|
||||
header.field.equiv("Content-Type")
|
||||
&& header.value.as_str().split(';').next() == Some("application/zip")
|
||||
}) {
|
||||
reply(request, 415, json!({"error": "Expected a ZIP archive"}));
|
||||
return;
|
||||
}
|
||||
let name = project_name(&request);
|
||||
let mut bytes = Vec::new();
|
||||
if let Err(error) = request
|
||||
.as_reader()
|
||||
.take(MAX_ZIP_UPLOAD + 1)
|
||||
.read_to_end(&mut bytes)
|
||||
{
|
||||
reply(request, 400, json!({"error": error.to_string()}));
|
||||
return;
|
||||
}
|
||||
if bytes.len() as u64 > MAX_ZIP_UPLOAD {
|
||||
reply(request, 413, json!({"error": "ZIP upload exceeds 64 MB"}));
|
||||
return;
|
||||
}
|
||||
match self.store.create_from_zip(&name, &bytes) {
|
||||
Ok(project) => reply(request, 201, json!(project)),
|
||||
Err(error) => reply(request, 422, json!({"error": error})),
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn create(&self, mut request: Request) {
|
||||
#[derive(serde::Deserialize)]
|
||||
struct NewProject {
|
||||
name: String,
|
||||
}
|
||||
if !request.headers().iter().any(|header| {
|
||||
header.field.equiv("Content-Type")
|
||||
&& header.value.as_str().split(';').next() == Some("application/json")
|
||||
}) {
|
||||
reply(
|
||||
request,
|
||||
415,
|
||||
json!({"error": "Expected JSON project details"}),
|
||||
);
|
||||
return;
|
||||
}
|
||||
let project: NewProject = match super::read_json(&mut request) {
|
||||
Ok(value) => value,
|
||||
Err(error) => {
|
||||
reply(request, 400, json!({"error": error}));
|
||||
return;
|
||||
}
|
||||
};
|
||||
match self.store.create_empty(&project.name) {
|
||||
Ok(project) => reply(request, 201, json!(project)),
|
||||
Err(error) => reply(request, 422, json!({"error": error})),
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn open(&self, id: &str) -> Result<EditorSession, Box<dyn std::error::Error>> {
|
||||
let root = self.store.project_dir(id)?;
|
||||
if fs::symlink_metadata(&root)?.file_type().is_symlink() {
|
||||
return Err("The project directory must be a local working copy".into());
|
||||
}
|
||||
let info = self.store.info(id)?;
|
||||
if info.main_file.is_empty() {
|
||||
return Err("The project does not contain a TeX entry file".into());
|
||||
}
|
||||
let source = self.store.file_path(id, &info.main_file)?.canonicalize()?;
|
||||
let files = info
|
||||
.files
|
||||
.iter()
|
||||
.filter(|file| file.path != info.main_file)
|
||||
.map(|file| PathBuf::from(&file.path))
|
||||
.collect::<Vec<_>>();
|
||||
let run = fresh_run(&self.runs, id)?;
|
||||
let mut session = EditorSession::new(run, |run| {
|
||||
prepare(
|
||||
&root,
|
||||
&source,
|
||||
&self.format,
|
||||
&self.manifest,
|
||||
run,
|
||||
&files,
|
||||
self.profile,
|
||||
)
|
||||
})?;
|
||||
session.id = Some(info.id);
|
||||
Ok(session)
|
||||
}
|
||||
}
|
||||
|
||||
fn fresh_run(parent: &Path, id: &str) -> std::io::Result<PathBuf> {
|
||||
for index in 1.. {
|
||||
let candidate = parent.join(format!("{id}-{}-{index}", std::process::id()));
|
||||
match fs::create_dir(&candidate) {
|
||||
Ok(()) => return Ok(candidate),
|
||||
Err(error) if error.kind() == ErrorKind::AlreadyExists => continue,
|
||||
Err(error) => return Err(error),
|
||||
}
|
||||
}
|
||||
unreachable!()
|
||||
}
|
||||
|
||||
fn project_name(request: &Request) -> String {
|
||||
let name = request
|
||||
.url()
|
||||
.split_once('?')
|
||||
.and_then(|(_, query)| query.split('&').find_map(|pair| pair.strip_prefix("name=")))
|
||||
.unwrap_or("Project");
|
||||
let mut bytes = Vec::with_capacity(name.len());
|
||||
let mut input = name.as_bytes().iter().copied();
|
||||
while let Some(byte) = input.next() {
|
||||
match byte {
|
||||
b'%' => {
|
||||
let hex = |byte: u8| char::from(byte).to_digit(16);
|
||||
if let Some(value) = input.next().and_then(hex).zip(input.next().and_then(hex)) {
|
||||
bytes.push((value.0 * 16 + value.1) as u8);
|
||||
}
|
||||
}
|
||||
b'+' => bytes.push(b' '),
|
||||
_ => bytes.push(byte),
|
||||
}
|
||||
}
|
||||
String::from_utf8_lossy(&bytes).into_owned()
|
||||
}
|
||||
Reference in new issue
Block a user