✨ Post with image / media
This commit is contained in:
@@ -4,6 +4,8 @@
|
|||||||
<img src="/image-broken.jpg" class="w-32 h-32 rounded-md" />
|
<img src="/image-broken.jpg" class="w-32 h-32 rounded-md" />
|
||||||
</template>
|
</template>
|
||||||
</n-image>
|
</n-image>
|
||||||
|
<audio v-else-if="itemType == 'audio'" :src="remoteSource" controls />
|
||||||
|
<video v-else-if="itemType == 'video'" :src="remoteSource" controls />
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
|
@@ -1,30 +1,58 @@
|
|||||||
<template>
|
<template>
|
||||||
<div class="flex flex-col gap-3">
|
<n-upload
|
||||||
<pub-select v-model:value="publisher" />
|
abstract
|
||||||
<n-input
|
with-credentials
|
||||||
type="textarea"
|
@remove="handleRemove"
|
||||||
placeholder="What's happended?!"
|
:create-thumbnail-url="createThumbnailUrl"
|
||||||
v-model:value="content"
|
:custom-request="customRequest"
|
||||||
@keydown.meta.enter.exact="submit"
|
v-model:file-list="fileList"
|
||||||
@keydown.ctrl.enter.exact="submit"
|
>
|
||||||
/>
|
<div class="flex flex-col gap-3">
|
||||||
<div class="flex justify-between">
|
<pub-select v-model:value="publisher" />
|
||||||
<div class="flex gap-2"></div>
|
<n-input
|
||||||
<n-button type="primary" icon-placement="right" :loading="submitting" @click="submit">
|
type="textarea"
|
||||||
Post
|
placeholder="What's happended?!"
|
||||||
<template #icon>
|
v-model:value="content"
|
||||||
<n-icon><send-round /></n-icon>
|
@keydown.meta.enter.exact="submit"
|
||||||
</template>
|
@keydown.ctrl.enter.exact="submit"
|
||||||
</n-button>
|
/>
|
||||||
|
<n-upload-file-list />
|
||||||
|
<div class="flex justify-between">
|
||||||
|
<div class="flex gap-2">
|
||||||
|
<n-upload-trigger #="{ handleClick }" abstract>
|
||||||
|
<n-button @click="handleClick">
|
||||||
|
<n-icon><upload-round /></n-icon>
|
||||||
|
</n-button>
|
||||||
|
</n-upload-trigger>
|
||||||
|
</div>
|
||||||
|
<n-button type="primary" icon-placement="right" :loading="submitting" @click="submit">
|
||||||
|
Post
|
||||||
|
<template #icon>
|
||||||
|
<n-icon><send-round /></n-icon>
|
||||||
|
</template>
|
||||||
|
</n-button>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</n-upload>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { NInput, NButton, NIcon } from 'naive-ui'
|
import {
|
||||||
|
NInput,
|
||||||
|
NButton,
|
||||||
|
NIcon,
|
||||||
|
NUpload,
|
||||||
|
NUploadFileList,
|
||||||
|
NUploadTrigger,
|
||||||
|
useMessage,
|
||||||
|
type UploadSettledFileInfo,
|
||||||
|
type UploadCustomRequestOptions,
|
||||||
|
create,
|
||||||
|
type UploadFileInfo,
|
||||||
|
} from 'naive-ui'
|
||||||
|
import { SendRound, UploadRound } from '@vicons/material'
|
||||||
import { ref } from 'vue'
|
import { ref } from 'vue'
|
||||||
|
import * as tus from 'tus-js-client'
|
||||||
import { SendRound } from '@vicons/material'
|
|
||||||
|
|
||||||
import PubSelect from './PubSelect.vue'
|
import PubSelect from './PubSelect.vue'
|
||||||
|
|
||||||
@@ -33,6 +61,8 @@ const emits = defineEmits(['posted'])
|
|||||||
const publisher = ref<string | undefined>()
|
const publisher = ref<string | undefined>()
|
||||||
const content = ref('')
|
const content = ref('')
|
||||||
|
|
||||||
|
const fileList = ref<UploadFileInfo[]>([])
|
||||||
|
|
||||||
const submitting = ref(false)
|
const submitting = ref(false)
|
||||||
|
|
||||||
async function submit() {
|
async function submit() {
|
||||||
@@ -44,11 +74,105 @@ async function submit() {
|
|||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
content: content.value,
|
content: content.value,
|
||||||
|
attachments: fileList.value
|
||||||
|
.filter((e) => e.url != null)
|
||||||
|
.map((e) => e.url!.split('/').reverse()[0]),
|
||||||
}),
|
}),
|
||||||
})
|
})
|
||||||
|
|
||||||
submitting.value = false
|
submitting.value = false
|
||||||
content.value = ''
|
content.value = ''
|
||||||
|
fileList.value = []
|
||||||
emits('posted')
|
emits('posted')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const messageDisplay = useMessage()
|
||||||
|
|
||||||
|
function customRequest({
|
||||||
|
file,
|
||||||
|
headers,
|
||||||
|
withCredentials,
|
||||||
|
onFinish,
|
||||||
|
onError,
|
||||||
|
onProgress,
|
||||||
|
}: UploadCustomRequestOptions) {
|
||||||
|
const requestHeaders: Record<string, string> = {}
|
||||||
|
const upload = new tus.Upload(file.file, {
|
||||||
|
endpoint: '/cgi/drive/tus',
|
||||||
|
retryDelays: [0, 3000, 5000, 10000, 20000],
|
||||||
|
removeFingerprintOnSuccess: false,
|
||||||
|
uploadDataDuringCreation: false,
|
||||||
|
metadata: {
|
||||||
|
filename: file.name,
|
||||||
|
'content-type': file.type ?? 'application/octet-stream',
|
||||||
|
},
|
||||||
|
headers: {
|
||||||
|
'X-DirectUpload': 'true',
|
||||||
|
...requestHeaders,
|
||||||
|
...headers,
|
||||||
|
},
|
||||||
|
onShouldRetry: () => false,
|
||||||
|
onError: function (error) {
|
||||||
|
if (error instanceof tus.DetailedError) {
|
||||||
|
const failedBody = error.originalResponse?.getBody()
|
||||||
|
if (failedBody != null)
|
||||||
|
messageDisplay.error(`Upload failed: ${failedBody}`, {
|
||||||
|
duration: 10000,
|
||||||
|
closable: true,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
console.error('[DRIVE] Upload failed:', error)
|
||||||
|
onError()
|
||||||
|
},
|
||||||
|
onProgress: function (bytesUploaded, bytesTotal) {
|
||||||
|
onProgress({ percent: (bytesUploaded / bytesTotal) * 100 })
|
||||||
|
},
|
||||||
|
onSuccess: function (payload) {
|
||||||
|
const rawInfo = payload.lastResponse.getHeader('x-fileinfo')
|
||||||
|
const jsonInfo = JSON.parse(rawInfo as string)
|
||||||
|
console.log('[DRIVE] Upload successful: ', jsonInfo)
|
||||||
|
file.url = `/cgi/drive/files/${jsonInfo.id}`
|
||||||
|
file.type = jsonInfo.mime_type
|
||||||
|
onFinish()
|
||||||
|
},
|
||||||
|
onBeforeRequest: function (req) {
|
||||||
|
const xhr = req.getUnderlyingObject()
|
||||||
|
xhr.withCredentials = withCredentials
|
||||||
|
},
|
||||||
|
})
|
||||||
|
upload.findPreviousUploads().then(function (previousUploads) {
|
||||||
|
if (previousUploads.length) {
|
||||||
|
upload.resumeFromPreviousUpload(previousUploads[0])
|
||||||
|
}
|
||||||
|
upload.start()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function createThumbnailUrl(
|
||||||
|
_file: File | null,
|
||||||
|
fileInfo: UploadSettledFileInfo,
|
||||||
|
): string | undefined {
|
||||||
|
if (!fileInfo) return undefined
|
||||||
|
return fileInfo.url ?? undefined
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleRemove(data: { file: UploadFileInfo; fileList: UploadFileInfo[] }) {
|
||||||
|
if (data.file.url == null) return true
|
||||||
|
const messageReactive = messageDisplay.loading('Deleting files...', {
|
||||||
|
duration: 0,
|
||||||
|
})
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
fetch(`/cgi/drive/files/${data.file.url!.split('/').reverse()[0]}`, { method: 'DELETE' })
|
||||||
|
.then(() => {
|
||||||
|
messageReactive.destroy()
|
||||||
|
messageDisplay.success('File has been deleted')
|
||||||
|
resolve(true)
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
messageReactive.destroy()
|
||||||
|
messageDisplay.error('Unable to delete this file: ' + err)
|
||||||
|
resolve(false)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
@@ -19,6 +19,10 @@ export default defineConfig({
|
|||||||
},
|
},
|
||||||
server: {
|
server: {
|
||||||
proxy: {
|
proxy: {
|
||||||
|
'/api/tus': {
|
||||||
|
target: 'http://localhost:5090',
|
||||||
|
changeOrigin: true,
|
||||||
|
},
|
||||||
'/api': {
|
'/api': {
|
||||||
target: 'http://localhost:5071',
|
target: 'http://localhost:5071',
|
||||||
changeOrigin: true,
|
changeOrigin: true,
|
||||||
|
Reference in New Issue
Block a user