Revert "Creates FileDownloader plugin to allow trigger file downloads with headers. #848."

This reverts commit c7685757dd.
This commit is contained in:
mateuswetah 2024-02-26 11:43:05 -03:00
parent c7685757dd
commit 65f96ff558
4 changed files with 6 additions and 65 deletions

View File

@ -280,7 +280,7 @@
<p>
<a
v-if="bgProcess.log"
@click="$fileDownloader.fetch(bgProcess.log)">
:href="bgProcess.log">
<span class="icon is-small">
<i class="tainacan-icon tainacan-icon-18px tainacan-icon-openurl"/>
</span>
@ -290,7 +290,7 @@
<a
v-if="bgProcess.error_log"
class="has-text-danger"
@click="$fileDownloader.fetch(bgProcess.error_log)">
:href="bgProcess.error_log">
<span class="icon is-small">
<i class="tainacan-icon tainacan-icon-18px tainacan-icon-openurl"/>
</span>

View File

@ -122,12 +122,12 @@
<br>
<a
v-if="bgProcess.log"
@click="$fileDownloader.fetch(bgProcess.log)">{{ $i18n.get('label_log_file') }}</a>
:href="bgProcess.log">{{ $i18n.get('label_log_file') }}</a>
<span v-if="bgProcess.error_log"> | </span>
<a
v-if="bgProcess.error_log"
class="has-text-danger"
@click="$fileDownloader.fetch(bgProcess.error_log)">{{ $i18n.get('label_error_log_file') }}</a>
:href="bgProcess.error_log">{{ $i18n.get('label_error_log_file') }}</a>
</div>
</li>
</ul>

View File

@ -83,8 +83,7 @@ import {
StatusHelperPlugin,
CommentsStatusHelperPlugin,
AdminOptionsHelperPlugin,
HtmlSanitizerPlugin,
FileDownloaderPlugin
HtmlSanitizerPlugin
} from './admin-utilities';
import {
ThumbnailHelperPlugin,
@ -177,7 +176,6 @@ export default (element) => {
Vue.use(ThumbnailHelperPlugin);
Vue.use(OrderByHelperPlugin);
Vue.use(StatusHelperPlugin);
Vue.use(FileDownloaderPlugin);
Vue.use(HtmlSanitizerPlugin);
Vue.use(ConsolePlugin, {visual: false});
Vue.use(VueTheMask);

View File

@ -133,6 +133,7 @@ I18NPlugin.install = function (Vue, options = {}) {
}
}
}
};
// USER PREFERENCES - Used to save key-value information for user settings of plugin
@ -375,64 +376,6 @@ UserCapabilitiesPlugin.install = function (Vue, options = {}) {
}
};
// FILE DOWNLOADER PLUGIN - Allows fetching a file passing proper headers.
export const FileDownloaderPlugin = {};
FileDownloaderPlugin.install = function (Vue, options = {}) {
Vue.prototype.$fileDownloader = {
async fetch(urlLink) {
try {
// Build heaaders from tainacan_plugin variable
let requestHeaders = {};
if (tainacan_plugin.nonce) {
requestHeaders['common'] = {};
requestHeaders['common']['X-WP-Nonce'] = tainacan_plugin.nonce;
}
if (tainacan_plugin.admin_request_options) {
Object.keys(tainacan_plugin.admin_request_options).forEach(requestOption => {
requestHeaders[requestOption] = tainacan_plugin.admin_request_options[requestOption];
});
}
// Async request for the file, passing propper headers
const response = await fetch(urlLink, {
headers: requestHeaders
});
// Creates an URL link in the form of blob:https://...
const blob = await response.blob();
const url = window.URL.createObjectURL(blob);
// Get the filename from the Content-Disposition header
const contentDisposition = response.headers.get('content-disposition');
let filename = 'log.txt';
if (contentDisposition) {
const matches = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/.exec(contentDisposition);
if (matches != null && matches[1])
filename = matches[1].replace(/['"]/g, '');
}
// Creates a temporal link and clicks it to download the file
const a = document.createElement('a');
a.href = url;
a.target = '_blank';
a.download = filename;
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
a.remove();
} catch (error) {
console.error('Tainacan received and error downloading file:', error);
}
}
}
};
// STATUS ICONS PLUGIN - Sets icon for status option
export const StatusHelperPlugin = {};
StatusHelperPlugin.install = function (Vue, options = {}) {