mirror of
https://github.com/mudabbir-ahmad/Media-Aggregation-and-Sorting-System.git
synced 2026-10-07 19:40:20 +00:00
added NAS configuration file; updated MediaViewer to support thumbnail carousel; improved error handling in NASAPI and NASAUTH; refactored various comments for clarity
This commit is contained in:
1 parent
9519b32d68
commit
f8e11fc368
27 files changed
+1036
-558
No files matched your search
@@ -0,0 +1,91 @@
|
||||
/**
|
||||
* Google Photos OAuth & API Test
|
||||
*
|
||||
* Usage:
|
||||
* node TEST/testGooglePhotosAPI.js <ACCESS_TOKEN>
|
||||
* GOOGLE_ACCESS_TOKEN=ya29.xxx node TEST/testGooglePhotosAPI.js
|
||||
*
|
||||
* How to get a token for manual testing:
|
||||
* 1. Go to https://developers.google.com/oauthplayground
|
||||
* 2. Select "Photos Library API v1" → photoslibrary scope
|
||||
* 3. Authorise the API
|
||||
* 4. Exchange the authorization code for tokens
|
||||
* 5. Copy the access token and pass it to this script
|
||||
*
|
||||
* Tests:
|
||||
* 1. Validates the token against the userinfo endpoint
|
||||
* 2. Lists media items from Google Photos
|
||||
* 3. Lists albums from Google Photos
|
||||
*/
|
||||
|
||||
const API_BASE = 'https://photoslibrary.googleapis.com/v1';
|
||||
const USERINFO_URL = 'https://www.googleapis.com/oauth2/v2/userinfo';
|
||||
|
||||
async function main() {
|
||||
const token = process.argv[2] || process.env.GOOGLE_ACCESS_TOKEN;
|
||||
|
||||
if (!token) {
|
||||
console.error('\nNo access token provided.');
|
||||
console.error('Usage: node TEST/testGooglePhotosAPI.js <ACCESS_TOKEN>');
|
||||
console.error('Or set: GOOGLE_ACCESS_TOKEN=ya29.xxx node TEST/testGooglePhotosAPI.js');
|
||||
console.error('\nTo obtain a token go to https://developers.google.com/oauthplayground');
|
||||
console.error('and authorise the Photos Library API v1 scope.\n');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const headers = { Authorization: `Bearer ${token}` };
|
||||
|
||||
console.log('\n--- Google Photos API Test ---\n');
|
||||
|
||||
// Test 1 — User info
|
||||
console.log('[1] Fetching user info...');
|
||||
try {
|
||||
const res = await fetch(USERINFO_URL, { headers });
|
||||
if (!res.ok) throw new Error(`HTTP ${res.status}: ${await res.text()}`);
|
||||
const user = await res.json();
|
||||
console.log(` Authenticated as: ${user.email} (${user.name})\n`);
|
||||
} catch (err) {
|
||||
console.log(` Failed: ${err.message}\n`);
|
||||
}
|
||||
|
||||
// Test 2 — List media items
|
||||
console.log('[2] Listing media items (first 10)...');
|
||||
try {
|
||||
const res = await fetch(`${API_BASE}/mediaItems?pageSize=10`, { headers });
|
||||
if (!res.ok) throw new Error(`HTTP ${res.status}: ${await res.text()}`);
|
||||
const data = await res.json();
|
||||
const items = data.mediaItems || [];
|
||||
console.log(` Found ${items.length} items`);
|
||||
items.slice(0, 5).forEach((item, i) => {
|
||||
console.log(` ${i + 1}. ${item.filename} (${item.mimeType})`);
|
||||
});
|
||||
if (items.length > 5) console.log(` ... and ${items.length - 5} more`);
|
||||
if (data.nextPageToken) console.log(' More pages available');
|
||||
console.log();
|
||||
} catch (err) {
|
||||
console.log(` Failed: ${err.message}\n`);
|
||||
}
|
||||
|
||||
// Test 3 — List albums
|
||||
console.log('[3] Listing albums...');
|
||||
try {
|
||||
const res = await fetch(`${API_BASE}/albums?pageSize=10`, { headers });
|
||||
if (!res.ok) throw new Error(`HTTP ${res.status}: ${await res.text()}`);
|
||||
const data = await res.json();
|
||||
const albums = data.albums || [];
|
||||
console.log(` Found ${albums.length} albums`);
|
||||
albums.slice(0, 5).forEach((album, i) => {
|
||||
console.log(` ${i + 1}. "${album.title}" - ${album.mediaItemsCount || 0} items`);
|
||||
});
|
||||
console.log();
|
||||
} catch (err) {
|
||||
console.log(` Failed: ${err.message}\n`);
|
||||
}
|
||||
|
||||
console.log('--- Tests complete ---\n');
|
||||
}
|
||||
|
||||
main().catch((err) => {
|
||||
console.error('Unhandled error:', err);
|
||||
process.exit(1);
|
||||
});
|
||||
@@ -0,0 +1,116 @@
|
||||
/**
|
||||
* NAS Backend API Test
|
||||
*
|
||||
* Usage:
|
||||
* node TEST/testNASAPI.js
|
||||
*
|
||||
* Environment variables:
|
||||
* BACKEND_URL (default http://localhost:3001)
|
||||
* NAS_HOST e.g. 192.168.1.100
|
||||
* NAS_PORT default 22
|
||||
* NAS_USER SSH username
|
||||
* NAS_PASS SSH password
|
||||
* NAS_FOLDER default /home/user/photos
|
||||
*
|
||||
* Tests:
|
||||
* 1. Backend health check
|
||||
* 2. SFTP connect
|
||||
* 3. List directory
|
||||
* 4. Disconnect
|
||||
*/
|
||||
|
||||
const BACKEND_URL = process.env.BACKEND_URL || 'http://localhost:3001';
|
||||
|
||||
async function main() {
|
||||
console.log('\n--- NAS Backend API Test ---');
|
||||
console.log(`Backend: ${BACKEND_URL}\n`);
|
||||
|
||||
// 1 — Health check
|
||||
console.log('[1] Health check...');
|
||||
try {
|
||||
const res = await fetch(`${BACKEND_URL}/api/health`);
|
||||
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
||||
const data = await res.json();
|
||||
console.log(` Status: ${data.status}, connections: ${data.activeConnections}, photo dir: ${data.defaultPhotoDir}\n`);
|
||||
} catch (err) {
|
||||
console.log(` Failed: ${err.message}`);
|
||||
console.log(' Start the backend first: cd ServerFiles && node server.js\n');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const host = process.env.NAS_HOST;
|
||||
const port = parseInt(process.env.NAS_PORT || '22', 10);
|
||||
const username = process.env.NAS_USER;
|
||||
const password = process.env.NAS_PASS;
|
||||
const folder = process.env.NAS_FOLDER || '/home/user/photos';
|
||||
|
||||
if (!host || !username || !password) {
|
||||
console.log('[2-4] Skipped — set NAS_HOST, NAS_USER, NAS_PASS to run connection tests.\n');
|
||||
console.log('--- Tests complete (partial) ---\n');
|
||||
return;
|
||||
}
|
||||
|
||||
// 2 — Connect
|
||||
let connectionToken = null;
|
||||
console.log(`[2] SFTP connect to ${host}:${port}...`);
|
||||
try {
|
||||
const res = await fetch(`${BACKEND_URL}/api/sftp/connect`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ host, port, username, password }),
|
||||
});
|
||||
if (!res.ok) throw new Error(`HTTP ${res.status}: ${await res.text()}`);
|
||||
const data = await res.json();
|
||||
if (!data.success) throw new Error(data.error || 'Connect failed');
|
||||
connectionToken = data.connectionToken;
|
||||
console.log(` Connected.\n`);
|
||||
} catch (err) {
|
||||
console.log(` Failed: ${err.message}\n`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const authHeaders = {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${connectionToken}`,
|
||||
};
|
||||
|
||||
// 3 — List directory
|
||||
console.log(`[3] List directory "${folder}"...`);
|
||||
try {
|
||||
const res = await fetch(`${BACKEND_URL}/api/sftp/list`, {
|
||||
method: 'POST',
|
||||
headers: authHeaders,
|
||||
body: JSON.stringify({ path: folder }),
|
||||
});
|
||||
if (!res.ok) throw new Error(`HTTP ${res.status}: ${await res.text()}`);
|
||||
const data = await res.json();
|
||||
console.log(` Found ${data.count || 0} items in ${data.path}`);
|
||||
(data.files || []).slice(0, 5).forEach((f, i) => {
|
||||
console.log(` ${i + 1}. ${f.name} ${f.isDirectory ? '(dir)' : `(${f.size} bytes)`}`);
|
||||
});
|
||||
console.log();
|
||||
} catch (err) {
|
||||
console.log(` Failed: ${err.message}\n`);
|
||||
}
|
||||
|
||||
// 4 — Disconnect
|
||||
console.log('[4] Disconnect...');
|
||||
try {
|
||||
const res = await fetch(`${BACKEND_URL}/api/sftp/disconnect`, {
|
||||
method: 'POST',
|
||||
headers: authHeaders,
|
||||
});
|
||||
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
||||
const data = await res.json();
|
||||
console.log(` ${data.message}\n`);
|
||||
} catch (err) {
|
||||
console.log(` Failed: ${err.message}\n`);
|
||||
}
|
||||
|
||||
console.log('--- Tests complete ---\n');
|
||||
}
|
||||
|
||||
main().catch((err) => {
|
||||
console.error('Unhandled error:', err);
|
||||
process.exit(1);
|
||||
});
|
||||
Reference in new issue
Block a user