Private
Public Access
Add image uploads in chat messages (Gitea issue #10)
Images live on the app server's local disk (uploads/), served through an authenticated, room-membership-gated endpoint since rooms can be private. Uploads are streamed with a byte-count cap, validated as genuine decodable images with Pillow (not just a spoofed Content-Type), and downscaled to 2000px on the longer side (except GIF, to preserve animation). Backend: MessageImage model + nullable Message.content/image_id with a content-or-image CheckConstraint, upload/serve endpoints in rooms.py, WS message envelope gains image_id, push notification body says "sent an image" for image-only messages. Frontend: Composer gets an attach button with upload progress and a thumbnail chip; MessageList renders images inline with a click-to-zoom ImageLightbox.
This commit is contained in:
1 parent
559adf9b7e
commit
f2a59f798b
27 files changed
+829
-40
No files matched your search
@@ -1,4 +1,4 @@
|
||||
import { apiFetch } from './client'
|
||||
import { apiFetch, ApiError, NetworkError } from './client'
|
||||
import type { Message, MyRoomItem, Room, RoomListItem, RoomMember, RoomRole } from '../types'
|
||||
|
||||
export function listRooms(): Promise<RoomListItem[]> {
|
||||
@@ -71,3 +71,39 @@ export function transferOwnership(roomId: string, newOwnerUserId: string): Promi
|
||||
export function getRoomMessages(roomId: string): Promise<Message[]> {
|
||||
return apiFetch<Message[]>(`/api/rooms/${roomId}/messages`)
|
||||
}
|
||||
|
||||
// Not apiFetch: that wrapper always sets Content-Type: application/json,
|
||||
// which would stomp the multipart boundary the browser needs to set itself
|
||||
// for a file upload.
|
||||
export async function uploadRoomImage(roomId: string, file: File): Promise<{ id: string }> {
|
||||
const formData = new FormData()
|
||||
formData.append('file', file)
|
||||
|
||||
let response: Response
|
||||
try {
|
||||
response = await fetch(`/api/rooms/${roomId}/images`, {
|
||||
method: 'POST',
|
||||
credentials: 'include',
|
||||
body: formData,
|
||||
})
|
||||
} catch {
|
||||
throw new NetworkError()
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
let detail = response.statusText
|
||||
try {
|
||||
const body = await response.json()
|
||||
detail = body.detail ?? detail
|
||||
} catch {
|
||||
// response had no JSON body
|
||||
}
|
||||
throw new ApiError(response.status, detail)
|
||||
}
|
||||
|
||||
return (await response.json()) as { id: string }
|
||||
}
|
||||
|
||||
export function getRoomImageUrl(roomId: string, imageId: string): string {
|
||||
return `/api/rooms/${roomId}/images/${imageId}`
|
||||
}
|
||||
Reference in new issue
Block a user