Skip to content

Commit

Permalink
Merge pull request #5 from daystram/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
daystram authored Jan 29, 2021
2 parents faa4495 + 1e82fc1 commit cff8e58
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 34 deletions.
67 changes: 50 additions & 17 deletions cut-be/src/app/controllers/v1/cut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ use crate::app::{
};
use crate::core::error::HandlerErrorKind;
use actix_form_data::Value;
use actix_web::{get, post, web, HttpRequest, HttpResponse, Responder};
use actix_web::{delete, get, post, web, HttpRequest, HttpResponse, Responder};

#[get("/{id}")]
#[get("/{hash}")]
pub async fn get_cut_raw(m: web::Data<Module>, req: HttpRequest) -> impl Responder {
let id: String = req.match_info().query("id").parse().unwrap();
match handlers::cut::get_one(m.clone(), id.clone()) {
let hash: String = req.match_info().query("hash").parse().unwrap();
match handlers::cut::get_one(m.clone(), hash.clone()) {
Ok(cut) => match cut.variant.as_str() {
constants::VARIANT_SNIPPET => HttpResponse::Ok()
.header("Content-Type", "text/plain")
Expand All @@ -19,7 +19,7 @@ pub async fn get_cut_raw(m: web::Data<Module>, req: HttpRequest) -> impl Respond
.header("Location", cut.data)
.finish(),
constants::VARIANT_FILE => {
let file = match handlers::cut::get_file(m, id) {
let file = match handlers::cut::get_file(m, hash) {
Ok(file) => file,
Err(e) => return HttpResponse::InternalServerError().body(format!("{:?}", e)),
};
Expand All @@ -41,6 +41,18 @@ pub async fn get_cut_raw(m: web::Data<Module>, req: HttpRequest) -> impl Respond
}
}

#[get("/{hash}")]
pub async fn get_cut(m: web::Data<Module>, req: HttpRequest) -> impl Responder {
let hash: String = req.match_info().query("hash").parse().unwrap();
match handlers::cut::get_one(m, hash) {
Ok(cut) => HttpResponse::Ok().json(cut),
Err(e) => match e.kind {
HandlerErrorKind::CutNotFoundError => HttpResponse::NotFound().finish(),
_ => HttpResponse::InternalServerError().body(format!("{:?}", e)),
},
}
}

#[get("/list")]
pub async fn get_cut_list(m: web::Data<Module>, req: HttpRequest) -> impl Responder {
let user: TokenInfo = match handlers::auth::authorize(&m, &req).await {
Expand All @@ -59,18 +71,6 @@ pub async fn get_cut_list(m: web::Data<Module>, req: HttpRequest) -> impl Respon
}
}

#[get("/{id}")]
pub async fn get_cut(m: web::Data<Module>, req: HttpRequest) -> impl Responder {
let id: String = req.match_info().query("id").parse().unwrap();
match handlers::cut::get_one(m, id) {
Ok(cut) => HttpResponse::Ok().json(cut),
Err(e) => match e.kind {
HandlerErrorKind::CutNotFoundError => HttpResponse::NotFound().finish(),
_ => HttpResponse::InternalServerError().body(format!("{:?}", e)),
},
}
}

#[post("")]
pub async fn post_snippet_create(
m: web::Data<Module>,
Expand Down Expand Up @@ -122,3 +122,36 @@ pub async fn post_file_upload(
Err(e) => HttpResponse::InternalServerError().body(format!("{:?}", e)),
}
}

#[delete("/{hash}")]
pub async fn delete_cut(m: web::Data<Module>, req: HttpRequest) -> impl Responder {
let hash: String = req.match_info().query("hash").parse().unwrap();
let user: TokenInfo = match handlers::auth::authorize(&m, &req).await {
Ok(res) => res,
Err(e) => match e.kind {
HandlerErrorKind::UnauthorizedError => return HttpResponse::Unauthorized().finish(),
_ => return HttpResponse::InternalServerError().finish(),
},
};
let cut = match handlers::cut::get_one(m.clone(), hash.clone()) {
Ok(cut) => {
if cut.owner != user.sub {
return HttpResponse::Unauthorized().finish();
}
cut
}
Err(e) => {
return match e.kind {
HandlerErrorKind::CutNotFoundError => HttpResponse::NotFound().finish(),
_ => HttpResponse::InternalServerError().body(format!("{:?}", e)),
}
}
};
match handlers::cut::delete(m, cut) {
Ok(_) => HttpResponse::Ok().finish(),
Err(e) => match e.kind {
HandlerErrorKind::CutNotFoundError => HttpResponse::NotFound().finish(),
_ => HttpResponse::InternalServerError().body(format!("{:?}", e)),
},
}
}
33 changes: 22 additions & 11 deletions cut-be/src/app/handlers/cut.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
use crate::app::datatransfers::cut_file::CutFile;
use crate::app::{
constants::{HASH_LENGTH, VARIANT_FILE},
datatransfers::cut::Cut,
Module,
use crate::{
app::{
constants::{HASH_LENGTH, VARIANT_FILE},
datatransfers::{cut::Cut, cut_file::CutFile},
Module,
},
core::error::{HandlerError, HandlerErrorKind},
utils::hash,
};
use crate::core::error::{HandlerError, HandlerErrorKind};
use crate::utils::hash;
use actix_web::web;
use r2d2_redis::redis::Commands;
use std::collections::HashMap;
use std::time::{SystemTime, UNIX_EPOCH};
use std::{
collections::HashMap,
time::{SystemTime, UNIX_EPOCH},
};

// Cut::file field not included
pub fn get_one(m: web::Data<Module>, hash: String) -> Result<Cut, HandlerError> {
Expand All @@ -35,7 +38,7 @@ pub fn get_one(m: web::Data<Module>, hash: String) -> Result<Cut, HandlerError>

pub fn get_file(m: web::Data<Module>, hash: String) -> Result<CutFile, HandlerError> {
let rd = &mut m.rd_pool.get()?;
let key_file = format!("cut-file::{}", hash.clone());
let key_file = format!("cut_file::{}", hash.clone());
let cut: Cut = match get_one(m, hash.clone()) {
Ok(cut) => cut,
Err(e) => return Err(e.into()),
Expand Down Expand Up @@ -84,7 +87,7 @@ pub fn insert(m: web::Data<Module>, mut cut: Cut) -> Result<String, HandlerError
let rd = &mut m.rd_pool.get()?;
cut.hash = hash::generate(HASH_LENGTH).into();
let key = format!("cut::{}", cut.hash.clone());
let key_file = format!("cut-file::{}", cut.hash.clone());
let key_file = format!("cut_file::{}", cut.hash.clone());
match rd.hset_multiple::<String, &str, Vec<u8>, String>(key.clone(), &cut.to_array()) {
Ok(_) => {
if cut.expiry >= 0 {
Expand Down Expand Up @@ -128,3 +131,11 @@ pub fn insert(m: web::Data<Module>, mut cut: Cut) -> Result<String, HandlerError
}
}
}
pub fn delete(m: web::Data<Module>, cut: Cut) -> Result<(), HandlerError> {
let rd = &mut m.rd_pool.get()?;
let key = format!("cut::{}", cut.hash);
rd.del::<String, i32>(key.clone())?;
rd.del::<String, i32>(format!("cut_file::{}", cut.hash))?;
rd.zrem::<String, String, i32>(format!("cut_file::{}", cut.owner), key)?;
Ok(())
}
3 changes: 2 additions & 1 deletion cut-be/src/app/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ pub fn init(app: &mut web::ServiceConfig) {
web::scope("/cut")
.service(v1::cut::get_cut_list)
.service(v1::cut::get_cut)
.service(v1::cut::post_snippet_create),
.service(v1::cut::post_snippet_create)
.service(v1::cut::delete_cut),
),
);
}
3 changes: 3 additions & 0 deletions cut-fe/src/apis/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ export default {
},
onUploadProgress
});
},
delete: function(hash: string): Promise<AxiosResponse> {
return apiClient.delete(`cut/${hash}`, withAuth());
}
}
};
4 changes: 4 additions & 0 deletions cut-fe/src/views/manage/Create.vue
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,10 @@ export default Vue.extend({
this.formLoadProgress =
(progressEvent.loaded / this.file.files[0].file.size) * 100;
}
},
beforeRouteLeave(to, from, next) {
if (this.formLoadStatus !== STATUS.LOADING) next();
}
});
</script>
84 changes: 79 additions & 5 deletions cut-fe/src/views/manage/List.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
hide-default-footer
class="elevation-4"
>
<template v-slot:no-data>No cuts created</template>
<template v-slot:item.name="{ item }">
<span
class="d-inline-block text-truncate"
Expand Down Expand Up @@ -104,13 +105,44 @@
</template>

<v-card width="420">
<v-card-title>
<v-row no-gutters align="center">
<v-col cols="auto">
Manage Cut
</v-col>
<v-spacer />
<v-col cols="auto">
<v-btn-toggle dense>
<v-btn
outlined
color="primary lighten-1"
:href="item.linkView"
target="_blank"
style="width: 50%"
>
View
</v-btn>
<v-btn
outlined
color="primary lighten-1"
:href="item.linkRaw"
target="_blank"
style="width: 50%"
>
Raw
</v-btn>
</v-btn-toggle>
</v-col>
</v-row>
</v-card-title>
<v-divider inset />
<div class="v-card__body pt-6">
<v-row align="center">
<v-col>
<div class="mb-4">
<div class="mb-1">
Use the following link to share your cut.
</div>
<div class="mt-4">
<div>
<v-text-field
v-model="item.linkView"
outlined
Expand All @@ -132,10 +164,10 @@
>
</v-text-field>
</div>
<div class="my-4">
<div class="mb-1">
To get view the raw cut, use the following.
</div>
<div class="mt-4">
<div>
<v-text-field
v-model="item.linkRaw"
outlined
Expand All @@ -158,6 +190,35 @@
</div>
</v-col>
</v-row>
<v-row>
<v-col>
<v-expand-transition>
<div v-show="item.formLoadStatus === STATUS.ERROR">
<v-alert
type="error"
text
dense
transition="scroll-y-transition"
class="mt-n2"
>
Failed deleting cut!
</v-alert>
</div>
</v-expand-transition>
<v-btn
dense
block
text
outlined
color="error"
:disabled="item.formLoadStatus === STATUS.LOADING"
:loading="item.formLoadStatus === STATUS.LOADING"
@click="deleteCut(item)"
>
Delete
</v-btn>
</v-col>
</v-row>
</div>
</v-card>
</v-menu>
Expand Down Expand Up @@ -244,7 +305,8 @@ export default Vue.extend({
raw: false,
viewTimeout: 0,
rawTimeout: 0
}
},
formLoadStatus: STATUS.IDLE
});
}
this.pageLoadStatus = STATUS.COMPLETE;
Expand Down Expand Up @@ -278,6 +340,18 @@ export default Vue.extend({
5000
);
}
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
deleteCut(item: any) {
item.formLoadStatus = STATUS.LOADING;
api.cut
.delete(item.hash)
.then(() => {
this.cuts.splice(this.cuts.indexOf(item), 1);
})
.catch(() => {
item.formLoadStatus = STATUS.IDLE;
});
}
}
});
Expand Down

0 comments on commit cff8e58

Please sign in to comment.