2021-10-05 16:21:07 +03:00
|
|
|
import Service from '@ember/service';
|
2023-04-07 08:52:37 +03:00
|
|
|
import fetch from 'fetch';
|
2021-10-05 16:21:07 +03:00
|
|
|
|
|
|
|
export default class UtilsService extends Service {
|
|
|
|
downloadFile(url) {
|
|
|
|
let iframe = document.getElementById('iframeDownload');
|
|
|
|
|
|
|
|
if (!iframe) {
|
|
|
|
iframe = document.createElement('iframe');
|
|
|
|
iframe.id = 'iframeDownload';
|
|
|
|
iframe.style.display = 'none';
|
|
|
|
document.body.append(iframe);
|
|
|
|
}
|
|
|
|
|
|
|
|
iframe.setAttribute('src', url);
|
|
|
|
}
|
2022-09-23 18:41:46 +03:00
|
|
|
|
2023-04-07 08:52:37 +03:00
|
|
|
/**
|
|
|
|
* This method will fetch a file from the server and then download it, resolving
|
|
|
|
* once the initial fetch is complete, allowing it to be used with loading spinners.
|
|
|
|
*
|
|
|
|
* @param {string} url - The URL of the file to download
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
async fetchAndDownloadFile(url) {
|
|
|
|
const response = await fetch(url);
|
|
|
|
const blob = await response.blob();
|
|
|
|
|
|
|
|
const anchor = document.createElement('a');
|
|
|
|
|
|
|
|
anchor.href = window.URL.createObjectURL(blob);
|
|
|
|
anchor.download = /filename="(.*)"/.exec(response.headers.get('Content-Disposition'))[1];
|
|
|
|
|
|
|
|
document.body.append(anchor);
|
|
|
|
|
|
|
|
anchor.click();
|
|
|
|
|
|
|
|
document.body.removeChild(anchor);
|
|
|
|
}
|
|
|
|
|
2022-09-23 18:41:46 +03:00
|
|
|
/**
|
|
|
|
* Remove tracking parameters from a URL
|
2023-01-13 13:08:54 +03:00
|
|
|
* @param {string} url
|
2022-09-23 18:41:46 +03:00
|
|
|
* @param {boolean} [display] Set to true to remove protocol and hash from the URL
|
2023-01-13 13:08:54 +03:00
|
|
|
* @returns
|
2022-09-23 18:41:46 +03:00
|
|
|
*/
|
|
|
|
cleanTrackedUrl(url, display = false) {
|
|
|
|
// Remove our own querystring parameters and protocol
|
2022-09-28 12:03:00 +03:00
|
|
|
const removeParams = ['ref', 'attribution_id', 'attribution_type'];
|
2022-09-23 18:41:46 +03:00
|
|
|
const urlObj = new URL(url);
|
|
|
|
for (const param of removeParams) {
|
|
|
|
urlObj.searchParams.delete(param);
|
|
|
|
}
|
|
|
|
if (!display) {
|
|
|
|
return urlObj.toString();
|
|
|
|
}
|
|
|
|
// Return URL without protocol
|
2023-01-13 13:08:54 +03:00
|
|
|
const urlWithoutProtocol = urlObj.host + (urlObj.pathname === '/' && !urlObj.search ? '' : urlObj.pathname) + (urlObj.search ? urlObj.search : '') + (urlObj.hash ? urlObj.hash : '');
|
|
|
|
// remove www. from the start of the URL
|
|
|
|
return urlWithoutProtocol.replace(/^www\./, '');
|
2022-09-23 18:41:46 +03:00
|
|
|
}
|
2021-10-05 16:21:07 +03:00
|
|
|
}
|