-
-
Notifications
You must be signed in to change notification settings - Fork 247
Expand file tree
/
Copy pathmain.py
More file actions
51 lines (47 loc) · 2.09 KB
/
Copy pathmain.py
File metadata and controls
51 lines (47 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# Copyright 2026 ledoent — Don Kendall
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
from odoo import http
from odoo.exceptions import AccessError, UserError
from odoo.http import request
class DmsLibreofficePreviewController(http.Controller):
@http.route(
"/dms/file/<int:file_id>/libreoffice_preview",
type="http",
auth="user",
methods=["GET"],
# Not readonly: cache-miss path writes a new ir.attachment via
# _ensure_libreoffice_preview(). Marking readonly=True triggers
# Odoo 19's "retry with r/w cursor" warning which checklog-odoo
# promotes to a build error.
)
def libreoffice_preview(self, file_id, **kwargs):
"""Serve a cached or freshly-converted PDF preview inline.
The `?v=<write_date>` query parameter is not validated server-side
— it exists purely as a cache-buster for the browser (the iframe
URL changes when `dms.file.write_date` changes, forcing a refetch
instead of pulling a stale render from the HTTP cache). Cache
validity on the server is owned by `_libreoffice_preview_attachment`.
"""
dms_file = request.env["dms.file"].browse(file_id).exists()
if not dms_file:
raise request.not_found()
try:
dms_file.check_access("read")
except AccessError as e:
raise request.not_found() from e
if not dms_file._libreoffice_preview_supported():
raise request.not_found()
try:
attachment = dms_file.sudo()._ensure_libreoffice_preview()
except UserError as e:
# Conversion errors are surfaced as 502 with the user-facing
# message — the side-pane handler can render this as an empty
# state via the existing error path.
return request.make_response(
str(e), status=502, headers=[("Content-Type", "text/plain")]
)
return (
request.env["ir.binary"]
._get_stream_from(attachment, "raw")
.get_response(as_attachment=False)
)