Split js into separate files
This commit is contained in:
94
js/open-service.js
Normal file
94
js/open-service.js
Normal file
@@ -0,0 +1,94 @@
|
||||
/* global window */
|
||||
|
||||
(function () {
|
||||
'use strict';
|
||||
|
||||
var Luxtools = window.Luxtools || (window.Luxtools = {});
|
||||
|
||||
// ============================================================
|
||||
// Open Service Module (file:// links)
|
||||
// ============================================================
|
||||
Luxtools.OpenService = (function () {
|
||||
function getServiceUrl(el) {
|
||||
var url = el.getAttribute('data-service-url') || '';
|
||||
url = (url || '').trim();
|
||||
if (!url) return '';
|
||||
// strip trailing slashes
|
||||
return url.replace(/\/+$/, '');
|
||||
}
|
||||
|
||||
function pingOpenViaImage(el, rawPath) {
|
||||
var baseUrl = getServiceUrl(el);
|
||||
if (!baseUrl) return;
|
||||
var url = baseUrl + '/open?path=' + encodeURIComponent(rawPath);
|
||||
|
||||
// Fire-and-forget without CORS.
|
||||
try {
|
||||
var img = new window.Image();
|
||||
img.src = url;
|
||||
} catch (e) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
|
||||
function openViaService(el, rawPath) {
|
||||
var baseUrl = getServiceUrl(el);
|
||||
if (!baseUrl) return Promise.reject(new Error('No client service configured'));
|
||||
|
||||
var headers = {
|
||||
'Content-Type': 'application/json'
|
||||
};
|
||||
|
||||
return window.fetch(baseUrl + '/open', {
|
||||
method: 'POST',
|
||||
mode: 'cors',
|
||||
credentials: 'omit',
|
||||
headers: headers,
|
||||
body: JSON.stringify({ path: rawPath })
|
||||
}).then(function (res) {
|
||||
if (!res.ok) {
|
||||
return res.json().catch(function () { return null; }).then(function (body) {
|
||||
var msg = (body && body.message) ? body.message : ('HTTP ' + res.status);
|
||||
throw new Error(msg);
|
||||
});
|
||||
}
|
||||
return res.json().catch(function () { return { ok: true }; });
|
||||
});
|
||||
}
|
||||
|
||||
function normalizeToFileUrl(path) {
|
||||
if (!path) return '';
|
||||
|
||||
// already a file URL
|
||||
if (/^file:\/\//i.test(path)) return path;
|
||||
|
||||
// UNC path: \\server\share\path
|
||||
if (/^\\\\/.test(path)) {
|
||||
var p = path.replace(/^\\\\/, '');
|
||||
p = p.replace(/\\/g, '/');
|
||||
return 'file://///' + p;
|
||||
}
|
||||
|
||||
// Windows drive: C:\path\to\file
|
||||
if (/^[a-zA-Z]:\\/.test(path)) {
|
||||
var drive = path[0].toUpperCase();
|
||||
var rest = path.slice(2).replace(/\\/g, '/');
|
||||
return 'file:///' + drive + ':' + rest;
|
||||
}
|
||||
|
||||
// POSIX absolute: /home/user/file
|
||||
if (path[0] === '/') {
|
||||
return 'file://' + path;
|
||||
}
|
||||
|
||||
// Fall back to using the provided string.
|
||||
return path;
|
||||
}
|
||||
|
||||
return {
|
||||
openViaService: openViaService,
|
||||
pingOpenViaImage: pingOpenViaImage,
|
||||
normalizeToFileUrl: normalizeToFileUrl
|
||||
};
|
||||
})();
|
||||
})();
|
||||
Reference in New Issue
Block a user