[REF] mail: clean attachment url

Part-of: odoo/odoo#121256
This commit is contained in:
Sébastien Theys
2023-05-15 12:56:09 +02:00
parent b1660a0b5c
commit f9611105e6
5 changed files with 48 additions and 44 deletions
@@ -8,6 +8,7 @@ import { sprintf } from "@web/core/utils/strings";
import { useAttachmentViewer } from "./attachment_viewer_hook";
import { _t } from "@web/core/l10n/translation";
import { url } from "@web/core/utils/urls";
/**
* @typedef {Object} Props
@@ -45,20 +46,14 @@ export class AttachmentList extends Component {
* @param {import("@mail/attachments/attachment_model").Attachment} attachment
*/
getImageUrl(attachment) {
const { imagesHeight } = this.props;
if (attachment.uploading && attachment.tmpUrl) {
return attachment.tmpUrl;
}
if (
!this.env.inComposer &&
!this.env.inChatter &&
!attachment.accessToken &&
attachment.originThread?.model === "discuss.channel"
) {
return `/discuss/channel/${attachment.originThread?.id}/image/${attachment.id}/${this.imagesWidth}x${imagesHeight}`;
}
const accessToken = attachment.accessToken ? `?access_token=${attachment.accessToken}` : "";
return `/web/image/${attachment.id}/${this.imagesWidth}x${imagesHeight}${accessToken}`;
return url(attachment.urlRoute, {
...attachment.urlQueryParams,
width: this.imagesWidth,
height: this.props.imagesHeight,
});
}
/**
@@ -1,5 +1,8 @@
/* @odoo-module */
import { assignDefined } from "@mail/utils/misc";
import { url } from "@web/core/utils/urls";
export class Attachment {
/** @type {import("@mail/core/store_service").Store} */
_store;
@@ -85,28 +88,10 @@ export class Attachment {
);
}
get imageUrl() {
if (!this.isImage) {
return "";
}
if (!this.accessToken && this.originThread?.model === "discuss.channel") {
return `/discuss/channel/${this.originThread.id}/image/${this.id}`;
}
const accessToken = this.accessToken ? `?access_token=${this.accessToken}` : "";
return `/web/image/${this.id}${accessToken}`;
}
get defaultSource() {
if (this.isImage) {
return `/web/image/${this.id}?signature=${this.checksum}`;
}
const route = url(this.urlRoute, this.urlQueryParams);
if (this.isPdf) {
const pdf_lib = "/web/static/lib/pdfjs/web/viewer.html?file=";
if (!this.accessToken && this.originThread?.model === "discuss.channel") {
return `${pdf_lib}/discuss/channel/${this.originThread.id}/attachment/${this.id}#pagemode=none`;
}
const accessToken = this.accessToken ? `?access_token%3D${this.accessToken}` : "";
return `${pdf_lib}/web/content/${this.id}${accessToken}#pagemode=none`;
return `/web/static/lib/pdfjs/web/viewer.html?file=${route}#pagemode=none`;
}
if (this.isUrlYoutube) {
const urlArr = this.url.split("/");
@@ -120,18 +105,42 @@ export class Attachment {
}
return `https://www.youtube.com/embed/${token}`;
}
if (!this.accessToken && this.originThread?.model === "discuss.channel") {
return `/discuss/channel/${this.originThread.id}/attachment/${this.id}`;
}
const accessToken = this.accessToken ? `?access_token=${this.accessToken}` : "";
return `/web/content/${this.id}${accessToken}`;
return route;
}
get downloadUrl() {
if (!this.accessToken && this.originThread?.model === "discuss.channel") {
return `/discuss/channel/${this.originThread.id}/attachment/${this.id}?download=true`;
return url(this.urlRoute, { ...this.urlQueryParams, download: true });
}
/**
* @returns {string}
*/
get urlRoute() {
if (this.uploading && this.tmpUrl) {
return this.tmpUrl;
}
const accessToken = this.accessToken ? `access_token=${this.accessToken}&` : "";
return `/web/content/ir.attachment/${this.id}/datas?${accessToken}download=true`;
if (!this.accessToken && this.originThread?.model === "discuss.channel") {
return this.isImage
? `/discuss/channel/${this.originThread.id}/image/${this.id}`
: `/discuss/channel/${this.originThread.id}/attachment/${this.id}`;
}
return this.isImage ? `/web/image/${this.id}` : `/web/content/ir.attachment/${this.id}`;
}
/**
* @returns {Object}
*/
get urlQueryParams() {
if (this.uploading && this.tmpUrl) {
return {};
}
return assignDefined(
{},
{
access_token: this.accessToken,
filename: this.name || undefined,
unique: this.checksum,
}
);
}
}
@@ -6,9 +6,9 @@
<t t-if="state.thread.mainAttachment">
<h3 t-if="!state.thread.mainAttachment.isPdf" class="mt0 mb8 ps-2 text-muted float-end"><t t-esc="state.thread.mainAttachment.filename"/></h3>
<div t-if="state.thread.mainAttachment.isImage" class="o-mail-Attachment-imgContainer">
<img id="attachment_img" class="img img-fluid d-block" t-attf-src="/web/image/#{state.thread.mainAttachment.id}?unique=1"/>
<img id="attachment_img" class="img img-fluid d-block" t-attf-src="state.thread.mainAttachment.defaultSource"/>
</div>
<iframe t-if="state.thread.mainAttachment.isPdf" class="mb48" t-attf-src="/web/static/lib/pdfjs/web/viewer.html?file=/web/content/#{state.thread.mainAttachment.id}?filename%3D#{window.encodeURIComponent(state.thread.mainAttachment.name)}" t-ref="iframeViewerPdf"/>
<iframe t-if="state.thread.mainAttachment.isPdf" class="mb48" t-attf-src="state.thread.mainAttachment.defaultSource" t-ref="iframeViewerPdf"/>
<t t-if="state.thread.attachmentsInWebClientView.length > 1">
<a class="arrow o_move_previous text-center" href="#" t-on-click.prevent="onClickPrevious">
<span class="oi oi-chevron-left"/>
@@ -226,7 +226,7 @@ export class AttachmentViewer extends Component {
</script>
</head>
<body onload='onloadImage()'>
<img src="${this.state.attachment.imageUrl}" alt=""/>
<img src="${this.state.attachment.defaultSource}" alt=""/>
</body>
</html>`);
printWindow.document.close();
@@ -31,7 +31,7 @@
<div t-if="!state.imageLoaded" class="position-absolute">
<i class="fa fa-3x fa-circle-o-notch fa-fw fa-spin text-white" role="img" title="Loading"/>
</div>
<img t-on-click.stop="" t-on-load="onImageLoaded" t-on-wheel="onWheelImage" t-on-mousedown.stop="onMousedownImage" t-on-mouseup.stop="onMouseupImage" class="o-mail-AttachmentViewer-view o-mail-AttachmentViewer-viewImage mw-100 mh-100 transition-base" t-att-src="state.attachment.imageUrl" t-att-style="imageStyle" draggable="false" alt="Viewer" t-ref="image"/>
<img t-on-click.stop="" t-on-load="onImageLoaded" t-on-wheel="onWheelImage" t-on-mousedown.stop="onMousedownImage" t-on-mouseup.stop="onMouseupImage" class="o-mail-AttachmentViewer-view o-mail-AttachmentViewer-viewImage mw-100 mh-100 transition-base" t-att-src="state.attachment.defaultSource" t-att-style="imageStyle" draggable="false" alt="Viewer" t-ref="image"/>
</div>
<iframe t-if="state.attachment.isPdf" class="o-mail-AttachmentViewer-view w-75 h-100 border-0" t-ref="iframeViewerPdf" t-att-class="{ 'w-100': env.isSmall }" t-att-src="state.attachment.defaultSource"/>
<iframe t-if="state.attachment.isText" class="o-mail-AttachmentViewer-view o-isText o_text w-75 h-100 border-0" t-att-src="state.attachment.defaultSource"/>