There is no public media any more. `/media/public/<id>` served a media row with no session at all, so anyone with the id could read a file. The public/private split is gone: the path is `/media/<id>` (and `/media/<id>/thumbnail`), every request is signed in first, and `grabUserMedia` decides whether the user may see it: their own upload, a bot's or workspace's picture or file they reach, or a file on a task they reach. Anything else is a 404 before a path is built. The `media.is_private` column and the two per-user directories went with it: uploads live in `media/<user_id>/` and there is nothing to pick between. The files browser's media volume is now the viewer's own folder whatever their role. Bots also say which of the two things they mean. A `[file:ID]` ref is a saved file and its chip opens the bytes; a folder is a new `[folder:volume:owner:path]` ref (`src/utils/folder-ref.ts`) whose chip opens the files browser there and downloads nothing. The old `[path:...]` ref and the extension sniffing that guessed file from folder are gone, so `import_file` names the folder holding a file it copied rather than the file itself. The prompts say what the Assistant got wrong: list before importing or saving, one copy per artifact, never split a file into parts unless asked, never claim the store is empty without a fresh list, and never invent a size cap. A re-import now says the path already existed instead of silently overwriting and reporting a fresh copy.
127 lines
3.7 KiB
TypeScript
127 lines
3.7 KiB
TypeScript
import twuiNumberfy from "@moduletrace/twui/utils/numberfy";
|
|
import loginUser from "@/src/functions/backend/auth/login-user";
|
|
import userAuth from "@/src/functions/backend/auth/user-auth";
|
|
import uploadAndRecordMedia from "@/src/functions/backend/db/users/media/upload-and-record-media";
|
|
import type { ApiReqParams } from "@/src/types";
|
|
import bufferFromBase64 from "@/src/utils/buffer-from-base-64";
|
|
import type {
|
|
APIResponseObject,
|
|
BunextAPIRouteHandler,
|
|
} from "@moduletrace/bunext/types";
|
|
import _ from "lodash";
|
|
import isReadOnlyUser from "@/src/utils/rbac/is-read-only-user";
|
|
import canUpdateUser from "@/src/utils/rbac/can-update-user";
|
|
import grabUserTypes from "@/src/functions/backend/db/users/grab-user-types";
|
|
|
|
export const handler: BunextAPIRouteHandler<APIResponseObject> = async (
|
|
params,
|
|
) => {
|
|
const req = params.req;
|
|
const body = params.body as ApiReqParams;
|
|
|
|
if (req.method !== "POST") {
|
|
return {
|
|
success: false,
|
|
};
|
|
}
|
|
|
|
const { user, user_types } = await userAuth({ req });
|
|
|
|
if (!user?.logged_in_status) {
|
|
return {
|
|
success: false,
|
|
msg: `Unauthorized`,
|
|
logoutUser: true,
|
|
};
|
|
}
|
|
|
|
try {
|
|
const {
|
|
user_id,
|
|
media_base_64_data_url,
|
|
media_paradigms,
|
|
media_type,
|
|
is_media_primary,
|
|
update_on_duplicate,
|
|
reauth,
|
|
return_media_text_content,
|
|
media,
|
|
media_name,
|
|
} = body;
|
|
|
|
/**
|
|
* Media is owned by the logged in user unless an actor with
|
|
* permission to update the target user uploads on their behalf.
|
|
*/
|
|
const target_user_id = user_id ? twuiNumberfy(user_id) : user.id;
|
|
const is_self = String(target_user_id) === String(user.id);
|
|
|
|
/**
|
|
* `read_only` users cannot modify records, but they may still manage
|
|
* their own profile image.
|
|
*/
|
|
if (isReadOnlyUser({ user_types })) {
|
|
const is_own_profile_image =
|
|
is_self && media_paradigms?.includes("user-profile-image");
|
|
|
|
if (!is_own_profile_image) {
|
|
throw new Error(`This user can only read records.`);
|
|
}
|
|
}
|
|
|
|
if (!is_self) {
|
|
const target_user_types = await grabUserTypes({
|
|
user_id: target_user_id,
|
|
});
|
|
|
|
const can_update_target = canUpdateUser({
|
|
actor_user_types: user_types,
|
|
actor_user_id: user.id,
|
|
target_user_id,
|
|
target_user_types,
|
|
});
|
|
|
|
if (!can_update_target) {
|
|
throw new Error(`Operation not allowed!`);
|
|
}
|
|
}
|
|
|
|
if (!media_base_64_data_url) {
|
|
throw new Error(
|
|
`No image payload sent! Base64 data URL with mime type is required.`,
|
|
);
|
|
}
|
|
|
|
const { buffer, ext } = await bufferFromBase64(media_base_64_data_url);
|
|
|
|
const new_media = await uploadAndRecordMedia({
|
|
data: buffer,
|
|
media_paradigms,
|
|
media_type,
|
|
is_primary: is_media_primary,
|
|
update_on_duplicate,
|
|
extract_media_text_content: return_media_text_content,
|
|
media,
|
|
media_mime_type: ext,
|
|
user: { id: target_user_id },
|
|
media_name,
|
|
});
|
|
|
|
if (reauth) {
|
|
return await loginUser({
|
|
user_id: Number(user.id),
|
|
req,
|
|
});
|
|
}
|
|
|
|
return {
|
|
...new_media,
|
|
};
|
|
} catch (error: any) {
|
|
return {
|
|
success: false,
|
|
msg: error.message,
|
|
};
|
|
}
|
|
};
|