uploading works! storing images separately and all
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
import { db } from "./db";
|
||||
import type { Delta } from "quill";
|
||||
|
||||
export interface Entry {
|
||||
@@ -20,19 +19,48 @@ function dataURLtoFile(dataurl: string, filename: string) {
|
||||
return new File([u8arr], filename, {type:mime});
|
||||
}
|
||||
|
||||
|
||||
async function uploadImages(delta: Delta) {
|
||||
let newDelta = delta
|
||||
|
||||
for (const value in newDelta.ops) {
|
||||
|
||||
}
|
||||
|
||||
async function uploadImage(b64: string) {
|
||||
const data = new FormData()
|
||||
data.append('image', dataURLtoFile('image.jpg', 'a'))
|
||||
|
||||
// unable to tell from the b64 whether it's a jpg or png but defaulting to jpg seems to work fine enough
|
||||
const file = dataURLtoFile(b64, 'image.jpg')
|
||||
data.append('image', file)
|
||||
|
||||
const r = await fetch('/api/image', {
|
||||
method: 'POST',
|
||||
body: data
|
||||
})
|
||||
|
||||
const url = (await r.json()).url
|
||||
return url
|
||||
}
|
||||
|
||||
export async function uploadDelta(entry: Entry) {
|
||||
async function uploadAllImages(delta: Delta) {
|
||||
let newDelta = delta
|
||||
|
||||
for (const val of newDelta.ops) {
|
||||
if (val.insert?.image != null) {
|
||||
const imgUrl = await uploadImage(val.insert!.image)
|
||||
val.insert!.image = imgUrl
|
||||
}
|
||||
}
|
||||
|
||||
return newDelta
|
||||
}
|
||||
|
||||
export async function uploadEntry(entry: Entry) {
|
||||
// first upload all the images seperately
|
||||
const delta = await uploadAllImages(entry.content)
|
||||
|
||||
const finalEntry: Entry = {
|
||||
date: entry.date,
|
||||
location: entry.location,
|
||||
content: delta
|
||||
}
|
||||
|
||||
const r = await fetch('/api/entry/new', {
|
||||
method: 'POST',
|
||||
headers: {'content-type': 'application/json'},
|
||||
body: JSON.stringify(finalEntry)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user