mirror of
https://github.com/trevor1969/teatracker.git
synced 2026-08-09 10:41:59 +00:00
fix: Add CORS proxy fallback for Nextcloud WebDAV connections
- Add NEXTCLOUD_PROXY constant for corsproxy.io - Add getProxiedUrl() method to NextcloudStorage class - Add fetchWithProxy() wrapper method that tries direct connection first, then falls back to CORS proxy if direct connection fails - Update all fetch() calls in NextcloudStorage to use fetchWithProxy() - Add useProxy flag to track proxy usage - This fixes 'NetworkError when attempting to fetch resource' for servers like wralto.org/nextcloud3 that have CORS restrictions Co-authored-by: trevor1969 <trevor1969@users.noreply.github.com>
This commit is contained in:
60
app.js
60
app.js
@ -12,6 +12,9 @@ const STORAGE_KEY_NC_CONFIG = 'teatracker_nextcloud_config';
|
|||||||
// Nextcloud Storage Integration
|
// Nextcloud Storage Integration
|
||||||
// ============================================
|
// ============================================
|
||||||
|
|
||||||
|
// CORS Proxy for Nextcloud WebDAV (fallback when direct connection fails due to CORS)
|
||||||
|
const NEXTCLOUD_PROXY = 'https://corsproxy.io/?';
|
||||||
|
|
||||||
class NextcloudStorage {
|
class NextcloudStorage {
|
||||||
constructor(baseUrl, username, password, path = '/TeeTracker/') {
|
constructor(baseUrl, username, password, path = '/TeeTracker/') {
|
||||||
this.baseUrl = baseUrl.replace(/\/$/, '');
|
this.baseUrl = baseUrl.replace(/\/$/, '');
|
||||||
@ -21,6 +24,7 @@ class NextcloudStorage {
|
|||||||
this.authHeader = 'Basic ' + btoa(`${username}:${password}`);
|
this.authHeader = 'Basic ' + btoa(`${username}:${password}`);
|
||||||
this.connected = false;
|
this.connected = false;
|
||||||
this.lastError = null;
|
this.lastError = null;
|
||||||
|
this.useProxy = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
getFileUrl(filename) {
|
getFileUrl(filename) {
|
||||||
@ -30,10 +34,54 @@ class NextcloudStorage {
|
|||||||
getDirectoryUrl() {
|
getDirectoryUrl() {
|
||||||
return `${this.baseUrl}/remote.php/dav/files/${encodeURIComponent(this.username)}${this.path}`;
|
return `${this.baseUrl}/remote.php/dav/files/${encodeURIComponent(this.username)}${this.path}`;
|
||||||
}
|
}
|
||||||
|
// Get proxied URL for CORS fallback
|
||||||
|
getProxiedUrl(url) {
|
||||||
|
return `${NEXTCLOUD_PROXY}${encodeURIComponent(url)}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wrapper for fetch that automatically uses proxy on CORS failure
|
||||||
|
async fetchWithProxy(url, options = {}) {
|
||||||
|
try {
|
||||||
|
// First try direct connection
|
||||||
|
const response = await fetch(url, options);
|
||||||
|
|
||||||
|
// If we get a CORS error or network error, it will throw
|
||||||
|
// If we get here, the request succeeded (even if status is not 2xx)
|
||||||
|
if (response.ok || response.status === 404 || response.status === 405) {
|
||||||
|
this.useProxy = false;
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Some errors might still occur, so try proxy
|
||||||
|
console.log('Direct connection returned non-ok status, trying proxy...');
|
||||||
|
} catch (error) {
|
||||||
|
// Direct connection failed (CORS or network error), try proxy
|
||||||
|
console.log('Direct connection failed, trying proxy...');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try with proxy
|
||||||
|
try {
|
||||||
|
const proxyUrl = this.getProxiedUrl(url);
|
||||||
|
this.useProxy = true;
|
||||||
|
const response = await fetch(proxyUrl, options);
|
||||||
|
|
||||||
|
if (response.ok || response.status === 404 || response.status === 405) {
|
||||||
|
console.log('Proxy connection successful');
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If proxy also fails, throw error
|
||||||
|
throw new Error(`Proxy returned status: ${response.status}`);
|
||||||
|
} catch (proxyError) {
|
||||||
|
this.useProxy = false;
|
||||||
|
console.error('Proxy connection also failed:', proxyError);
|
||||||
|
throw new Error(`Both direct and proxy connections failed: ${error.message || ''} ${proxyError.message || ''}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async testConnection() {
|
async testConnection() {
|
||||||
try {
|
try {
|
||||||
const response = await fetch(this.getDirectoryUrl(), {
|
const response = await this.fetchWithProxy(this.getDirectoryUrl(), {
|
||||||
method: 'PROPFIND',
|
method: 'PROPFIND',
|
||||||
headers: {
|
headers: {
|
||||||
'Authorization': this.authHeader,
|
'Authorization': this.authHeader,
|
||||||
@ -65,7 +113,7 @@ class NextcloudStorage {
|
|||||||
|
|
||||||
async ensureDirectory() {
|
async ensureDirectory() {
|
||||||
try {
|
try {
|
||||||
const response = await fetch(this.getDirectoryUrl(), {
|
const response = await this.fetchWithProxy(this.getDirectoryUrl(), {
|
||||||
method: 'MKCOL',
|
method: 'MKCOL',
|
||||||
headers: { 'Authorization': this.authHeader }
|
headers: { 'Authorization': this.authHeader }
|
||||||
});
|
});
|
||||||
@ -80,7 +128,7 @@ class NextcloudStorage {
|
|||||||
async ensureSubDirectory(subPath) {
|
async ensureSubDirectory(subPath) {
|
||||||
try {
|
try {
|
||||||
const subDirUrl = `${this.getDirectoryUrl()}${subPath}/`;
|
const subDirUrl = `${this.getDirectoryUrl()}${subPath}/`;
|
||||||
const response = await fetch(subDirUrl, {
|
const response = await this.fetchWithProxy(subDirUrl, {
|
||||||
method: 'MKCOL',
|
method: 'MKCOL',
|
||||||
headers: { 'Authorization': this.authHeader }
|
headers: { 'Authorization': this.authHeader }
|
||||||
});
|
});
|
||||||
@ -94,7 +142,7 @@ class NextcloudStorage {
|
|||||||
async loadFile(filename) {
|
async loadFile(filename) {
|
||||||
try {
|
try {
|
||||||
const url = this.getFileUrl(filename);
|
const url = this.getFileUrl(filename);
|
||||||
const response = await fetch(url, {
|
const response = await this.fetchWithProxy(url, {
|
||||||
headers: { 'Authorization': this.authHeader }
|
headers: { 'Authorization': this.authHeader }
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -114,7 +162,7 @@ class NextcloudStorage {
|
|||||||
async saveFile(filename, data) {
|
async saveFile(filename, data) {
|
||||||
try {
|
try {
|
||||||
const url = this.getFileUrl(filename);
|
const url = this.getFileUrl(filename);
|
||||||
const response = await fetch(url, {
|
const response = await this.fetchWithProxy(url, {
|
||||||
method: 'PUT',
|
method: 'PUT',
|
||||||
headers: {
|
headers: {
|
||||||
'Authorization': this.authHeader,
|
'Authorization': this.authHeader,
|
||||||
@ -132,7 +180,7 @@ class NextcloudStorage {
|
|||||||
async fileExists(filename) {
|
async fileExists(filename) {
|
||||||
try {
|
try {
|
||||||
const url = this.getFileUrl(filename);
|
const url = this.getFileUrl(filename);
|
||||||
const response = await fetch(url, {
|
const response = await this.fetchWithProxy(url, {
|
||||||
method: 'HEAD',
|
method: 'HEAD',
|
||||||
headers: { 'Authorization': this.authHeader }
|
headers: { 'Authorization': this.authHeader }
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user