Update
continuous-integration/drone/push Build is failing Details

This commit is contained in:
Dominic Grimm 2023-01-30 17:46:43 +01:00
parent 4f6d30dc4b
commit 1c164d6ed2
No known key found for this signature in database
GPG Key ID: 6F294212DEAAC530
7 changed files with 100 additions and 44 deletions

View File

@ -16,10 +16,10 @@ futures = "0.3.25"
lazy_static = "1.4.0"
mime = "0.3.16"
serde = { version = "1.0.152", features = ["derive"] }
tempfile = "3.3.0"
untis = { git = "https://git.dergrimm.net/dergrimm/untis.rs.git", branch = "main" }
url = "2.3.1"
uuid = { version = "1.2.2", features = ["v4", "fast-rng", "macro-diagnostics"] }
wkhtmltopdf = "0.4.0"
[target.'cfg(not(target_env = "msvc"))'.dependencies]
tikv-jemallocator = "0.5"

View File

@ -6,17 +6,17 @@ RUN mkdir src && touch src/main.rs
COPY ./Cargo.toml ./Cargo.lock ./
RUN cargo chef prepare --recipe-path recipe.json
FROM alpine as wkhtmltopdf
WORKDIR /tmp
RUN wget -O wkhtmltopdf.deb https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6.1-2/wkhtmltox_0.12.6.1-2.bullseye_amd64.deb
# FROM alpine as wkhtmltopdf
# WORKDIR /tmp
# RUN wget -O wkhtmltopdf.deb https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6.1-2/wkhtmltox_0.12.6.1-2.bullseye_amd64.deb
FROM chef as builder
WORKDIR /tmp
RUN apt update
RUN apt install -y xfonts-base xfonts-75dpi
COPY --from=wkhtmltopdf /tmp/wkhtmltopdf.deb .
RUN dpkg -i wkhtmltopdf.deb
RUN rm wkhtmltopdf.deb
# RUN apt install -y xfonts-base xfonts-75dpi
# COPY --from=wkhtmltopdf /tmp/wkhtmltopdf.deb .
# RUN dpkg -i wkhtmltopdf.deb
# RUN rm wkhtmltopdf.deb
RUN apt-get clean
RUN apt-get autoremove -y
WORKDIR /usr/src/auth
@ -29,11 +29,12 @@ FROM docker.io/debian:bullseye-slim as runner
WORKDIR /tmp
RUN apt update
RUN apt install -y ca-certificates
RUN apt install -y wget xfonts-base xfonts-75dpi fontconfig libjpeg62-turbo libx11-6 libxcb1 libxext6 libxrender1
COPY --from=wkhtmltopdf /tmp/wkhtmltopdf.deb .
RUN dpkg -i wkhtmltopdf.deb
RUN rm wkhtmltopdf.deb
RUN apt-get clean
RUN apt install -y wget
RUN wget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-1/wkhtmltox_0.12.6-1.buster_amd64.deb
RUN apt install -y ./wkhtmltox_0.12.6-1.buster_amd64.deb
RUN rm wkhtmltox_0.12.6-1.buster_amd64.deb
RUN apt remove -y wget
RUN apt clean
RUN apt-get autoremove -y
RUN rm -rf /var/lib/{apt,dpkg,cache,log}/
WORKDIR /usr/src/auth

View File

@ -1,6 +1,12 @@
use actix_web::{post, web::Json, HttpResponse};
use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::time::{SystemTime, UNIX_EPOCH};
use std::fs::File;
use std::process::Command;
use std::{
io::Write,
time::{SystemTime, UNIX_EPOCH},
};
use uuid::Uuid;
#[derive(Deserialize, Debug)]
@ -14,38 +20,30 @@ pub struct PostPdfResponse {
pub filename: Option<String>,
}
fn gen_pdf(html: &str, path: &str) -> Result<()> {
let dir = tempfile::tempdir()?;
let file_path = dir.path().join("content.html");
let mut file = File::create(&file_path)?;
file.write_all(html.as_bytes())?;
Command::new("wkhtmltopdf")
.arg(file_path)
.arg(path)
.output()?;
dir.close()?;
Ok(())
}
#[post("/v1/pdf")]
async fn post_pdf(data: Json<PostPdfRequest>) -> HttpResponse {
let pdf_app = match wkhtmltopdf::PdfApplication::new() {
Ok(x) => x,
Err(x) => {
return HttpResponse::InternalServerError().json(PostPdfResponse {
error: Some(x.to_string()),
filename: None,
})
}
};
let mut pdfout = match pdf_app
.builder()
.orientation(wkhtmltopdf::Orientation::Portrait)
.margin(wkhtmltopdf::Size::Millimeters(10))
.build_from_html(&data.html)
{
Ok(x) => x,
Err(x) => {
return HttpResponse::InternalServerError().json(PostPdfResponse {
error: Some(x.to_string()),
filename: None,
})
}
};
let start = SystemTime::now();
let since_epoch = start
.duration_since(UNIX_EPOCH)
.expect("Time went backwards");
let filename = format!("/static/{}_{}.pdf", since_epoch.as_secs(), Uuid::new_v4());
if let Err(x) = pdfout.save(&filename) {
if let Err(x) = gen_pdf(&data.html, &filename) {
return HttpResponse::InternalServerError().json(PostPdfResponse {
error: Some(x.to_string()),
filename: None,

View File

@ -41,7 +41,7 @@ fn escape_username(s: &str) -> String {
)
}
async fn students(
async fn classes(
client: &mut untis::Client,
usernames: &mut HashMap<String, usize>,
) -> Result<Vec<Class>> {
@ -70,7 +70,11 @@ async fn students(
.await?
.into_iter()
.map(|c| Class {
name: c.name,
name: c
.name
.chars()
.filter(|c| !c.is_whitespace())
.collect::<String>(),
students: c
.students
.into_iter()
@ -155,7 +159,7 @@ pub async fn get_users() -> HttpResponse {
}
let mut usernames = HashMap::<String, usize>::new();
let classes = match students(&mut client, &mut usernames).await {
let classes = match classes(&mut client, &mut usernames).await {
Ok(x) => x,
Err(x) => {
return HttpResponse::InternalServerError().json(GetStudentsResponse {

View File

@ -191,6 +191,7 @@ module Backend
{% end %}
else
{% if !flag?(:release) %}
p! ex
ex.message || Errors::UNKNOWN_PRIVATE_ERROR
{% else %}
Errors::UNKNOWN_PRIVATE_ERROR

View File

@ -62,11 +62,13 @@ module Backend
end
cmd.commands.add do |c|
c.use = "seed"
c.use = "user:import"
c.short = "Imports users from Untis"
c.long = c.short
c.run do
time = Time.local
users = Auth.users
students = [] of Templates::Users::Student
@ -121,7 +123,7 @@ module Backend
)
html = Templates::Users.new(
Time.local,
time,
users.classes.map { |cl| {cl.name, cl.students.size} },
students,
teachers
@ -157,6 +159,55 @@ module Backend
end
end
# ameba:disable Lint/ShadowingOuterLocalVar
cmd.commands.add do |cmd|
cmd.use = "user:export"
cmd.short = "Generates report for all users and resets their password"
cmd.long = cmd.short
cmd.run do
print "Do you really want to reset all passwords? [y/N] "
exit unless gets(chomp: true).not_nil!.strip.to_b
time = Time.local
students = [] of Templates::Users::Student
teachers = [] of Templates::Users::User
Db::User.query.each do |user|
password = Password.generate(Password::DEFAULT_LEN)
user.password = password
user.save!
case user.role.to_api
in Api::Schema::UserRole::Student
students << Templates::Users::Student.new(
class_name: user.student.not_nil!.class_model.name,
user: Templates::Users::User.new(
first_name: user.first_name,
last_name: user.last_name,
username: user.username,
password: password
)
)
in Api::Schema::UserRole::Teacher
teachers << Templates::Users::User.new(
first_name: user.first_name,
last_name: user.last_name,
username: user.username,
password: password
)
end
end
html = Templates::Users.new(
time,
Db::Class.query.with_students.to_a.map { |cl| {cl.name, cl.students.count.to_i32} },
students,
teachers
).to_s
puts "Filepath: #{Auth.generate_pdf(html).filename}"
end
end
# ameba:disable Lint/ShadowingOuterLocalVar
cmd.commands.add do |cmd|
cmd.use = "user:admin <id> <admin>"

View File

@ -32,6 +32,7 @@ services:
db:
image: docker.io/postgres:alpine
restart: always
command: postgres -c "max_connections=256"
environment:
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}