This commit is contained in:
j3d1 2023-12-13 00:54:59 +01:00
parent cb735e7880
commit 0434961829
5 changed files with 59 additions and 12 deletions

View file

@ -39,10 +39,14 @@ def thumbnail_urls(request, size, hash):
from PIL import Image from PIL import Image
image = Image.open(file.file) image = Image.open(file.file)
image.thumbnail((size, size)) image.thumbnail((size, size))
image.save(MEDIA_ROOT + f'/media/thumbnails/{size}/{hash_path}', quality=90) rgb_image = image.convert('RGB')
thumb_dir = os.path.dirname(MEDIA_ROOT + f'/thumbnails/{size}/{hash_path}')
if not os.path.exists(thumb_dir):
os.makedirs(thumb_dir)
rgb_image.save(MEDIA_ROOT + f'/thumbnails/{size}/{hash_path}', 'jpeg', quality=90)
return HttpResponse(status=status.HTTP_200_OK, return HttpResponse(status=status.HTTP_200_OK,
content_type=file.mime_type, content_type="image/jpeg",
headers={ headers={
'X-Accel-Redirect': f'/redirect_thumbnail/{size}/{hash_path}', 'X-Accel-Redirect': f'/redirect_thumbnail/{size}/{hash_path}',
'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Origin': '*',

View file

@ -0,0 +1,39 @@
<template>
<img :src="image_data" :alt="src">
</template>
<style scoped>
</style>
<script>
import {mapActions} from "vuex";
export default {
name: "AuthenticatedImage",
props: {
src: {
type: String,
required: true
},
},
data() {
return {
image_data: "",
servers: []
}
},
methods: mapActions(['fetchImage']),
async mounted() {
try {
const response = await this.fetchImage(this.src);
const mime_type = response.headers.get("content-type");
const base64 = btoa(new Uint8Array(await response.arrayBuffer())
.reduce((data, byte) => data + String.fromCharCode(byte), ""));
this.image_data = "data:" + mime_type + ";base64," + base64;
} catch (e) {
console.log(e);
}
}
}
</script>

View file

@ -1,12 +1,12 @@
<template> <template>
<Modal @close="$emit('close')"> <Modal @close="$emit('close')">
<template #body> <template #body>
<img <AuthenticatedImage
class="img-fluid rounded mx-auto d-block mb-3 w-100" class="img-fluid rounded mx-auto d-block mb-3 w-100"
:src="`/media/2/${file}/`" :src="`/media/2/${file}/`"
alt="Image not available." alt="Image not available."
id="lightbox-image" id="lightbox-image"
> />
</template> </template>
<template #buttons> <template #buttons>
<button type="button" class="btn btn-secondary" @click="$emit('close')">Cancel</button> <button type="button" class="btn btn-secondary" @click="$emit('close')">Cancel</button>

View file

@ -202,10 +202,13 @@ const store = new Vuex.Store({
store.commit('logout'); store.commit('logout');
router.push('/login'); router.push('/login');
}, },
async afterLogin() { async afterLogin({dispatch}) {
await store.dispatch('loadBoxes'); await dispatch('loadBoxes');
await store.dispatch('loadEventItems'); await dispatch('loadEventItems');
await store.dispatch('loadTickets'); await dispatch('loadTickets');
},
async fetchImage({state}, url) {
return await fetch(url, {headers: {'Authorization': `Token ${state.token}`}}).then(r => r.blob());
}, },
async loadEvents({commit}) { async loadEvents({commit}) {
const {data} = await axios.get('/2/events/'); const {data} = await axios.get('/2/events/');

View file

@ -44,10 +44,10 @@
v-slot="{ item }" v-slot="{ item }"
@itemActivated="openLightboxModalWith($event)" @itemActivated="openLightboxModalWith($event)"
> >
<img <AuthenticatedImage
:src="`/media/2/200/${item.file}/`" :src="`/media/2/256/${item.file}/`"
class="card-img-top img-fluid" class="card-img-top img-fluid"
> />
<div class="card-body"> <div class="card-body">
<h6 class="card-title">{{ item.description }}</h6> <h6 class="card-title">{{ item.description }}</h6>
<h6 class="card-subtitle text-secondary">uid: {{ item.uid }} box: {{ item.box }}</h6> <h6 class="card-subtitle text-secondary">uid: {{ item.uid }} box: {{ item.box }}</h6>
@ -76,6 +76,7 @@ import Modal from '@/components/Modal';
import EditItem from '@/components/EditItem'; import EditItem from '@/components/EditItem';
import {mapActions, mapState} from 'vuex'; import {mapActions, mapState} from 'vuex';
import Lightbox from '../components/Lightbox'; import Lightbox from '../components/Lightbox';
import AuthenticatedImage from "@/components/AuthenticatedImage.vue";
export default { export default {
name: 'Items', name: 'Items',
@ -83,7 +84,7 @@ export default {
lightboxItem: null, lightboxItem: null,
editingItem: null, editingItem: null,
}), }),
components: {Lightbox, Table, Cards, Modal, EditItem}, components: {AuthenticatedImage, Lightbox, Table, Cards, Modal, EditItem},
computed: mapState(['loadedItems', 'layout']), computed: mapState(['loadedItems', 'layout']),
methods: { methods: {
...mapActions(['deleteItem', 'markItemReturned']), ...mapActions(['deleteItem', 'markItemReturned']),