mirror of
https://github.com/thiloho/archtika.git
synced 2025-11-22 02:41:35 +01:00
Ability to bulk import or export articles as gzip, handle domain prefix logic in API and other smaller improvements
This commit is contained in:
49
rest-api/db/migrations/20241029160539_export_articles.sql
Normal file
49
rest-api/db/migrations/20241029160539_export_articles.sql
Normal file
@@ -0,0 +1,49 @@
|
||||
-- migrate:up
|
||||
CREATE FUNCTION api.export_articles_zip (website_id UUID)
|
||||
RETURNS "*/*"
|
||||
AS $$
|
||||
DECLARE
|
||||
_has_access BOOLEAN;
|
||||
_headers TEXT;
|
||||
_article RECORD;
|
||||
_markdown_dir TEXT := '/tmp/website-' || export_articles_zip.website_id;
|
||||
BEGIN
|
||||
_has_access = internal.user_has_website_access (export_articles_zip.website_id, 20);
|
||||
|
||||
SELECT
|
||||
FORMAT('[{ "Content-Type": "application/gzip" },'
|
||||
'{ "Content-Disposition": "attachment; filename=\"%s\"" }]',
|
||||
'archtika-export-articles-' || export_articles_zip.website_id || '.tar.gz') INTO _headers;
|
||||
PERFORM
|
||||
SET_CONFIG('response.headers', _headers, TRUE);
|
||||
|
||||
EXECUTE FORMAT('COPY (SELECT '''') TO PROGRAM ''mkdir -p %s''', _markdown_dir || '/articles');
|
||||
|
||||
FOR _article IN (
|
||||
SELECT a.id, a.website_id, a.slug, a.main_content
|
||||
FROM internal.article AS a
|
||||
WHERE a.website_id = export_articles_zip.website_id)
|
||||
LOOP
|
||||
EXECUTE FORMAT(
|
||||
'COPY (SELECT %L) TO ''%s''',
|
||||
_article.main_content,
|
||||
_markdown_dir || '/articles/' || _article.slug || '.md'
|
||||
);
|
||||
END LOOP;
|
||||
|
||||
EXECUTE FORMAT('COPY (SELECT '''') TO PROGRAM ''tar -czf %s -C %s articles && rm %s''',
|
||||
_markdown_dir || '/export.tar.gz',
|
||||
_markdown_dir,
|
||||
_markdown_dir || '/articles/*.md'
|
||||
);
|
||||
|
||||
RETURN pg_read_binary_file(_markdown_dir || '/export.tar.gz');
|
||||
END;
|
||||
$$
|
||||
LANGUAGE plpgsql
|
||||
SECURITY DEFINER;
|
||||
|
||||
GRANT EXECUTE ON FUNCTION api.export_articles_zip TO authenticated_user;
|
||||
|
||||
-- migrate:down
|
||||
DROP FUNCTION api.export_articles_zip;
|
||||
Reference in New Issue
Block a user