It made no sense to have a file upload in each form that needs a media, because to reuse an existing media users would need to upload the exact same file again; this is very unusual and unfriendly. A better option is to have a “centralized” media section, where people can upload files there, and then have a picker to select from there. Ideally, there would be an upload option in the picker, but i did not add it yet. I’ve split the content from the media because i want users to have the option to update a media, for instance when they need to upload a reduced or cropped version of the same photo, without an edit they would need to upload the file as a new media and then update all places where the old version was used. And i did not want to trouble people that uploads the same photo twice: without the separate relation, doing so would throw a constraint error. I do not believe there is any security problem to have all companies link their media to the same file, as they were already readable by everyone and could upload the data from a different company to their own; in other words, it is not worse than it was now.
42 lines
979 B
PL/PgSQL
42 lines
979 B
PL/PgSQL
-- Deploy camper:edit_media to pg
|
|
-- requires: roles
|
|
-- requires: schema_camper
|
|
-- requires: media_content
|
|
-- requires: media
|
|
-- requires: media_type
|
|
|
|
begin;
|
|
|
|
set search_path to camper, public;
|
|
|
|
create or replace function edit_media(media_id integer, filename text, media_type media_type default null, content bytea default null) returns integer as
|
|
$$
|
|
declare
|
|
hash bytea;
|
|
begin
|
|
if content is not null then
|
|
insert into media_content (media_type, bytes)
|
|
values (media_type, content)
|
|
on conflict (content_hash) do update
|
|
set media_type = excluded.media_type
|
|
returning content_hash into hash
|
|
;
|
|
end if;
|
|
|
|
update media
|
|
set original_filename = filename
|
|
, content_hash = coalesce(hash, content_hash)
|
|
where media.media_id = edit_media.media_id
|
|
;
|
|
|
|
return media_id;
|
|
end
|
|
$$
|
|
language plpgsql
|
|
;
|
|
|
|
revoke execute on function edit_media(integer, text, media_type, bytea) from public;
|
|
grant execute on function edit_media(integer, text, media_type, bytea) to admin;
|
|
|
|
commit;
|