265 lines
5.9 KiB
TypeScript
265 lines
5.9 KiB
TypeScript
'use server';
|
|
|
|
import { getSessionData } from '@/app/actions/session';
|
|
import { API_BASE_URL } from '@/app/actions/definitions';
|
|
import { headers } from 'next/headers';
|
|
|
|
export async function requestFileWithTracking(fileId: string) {
|
|
const token = await getSessionData('token');
|
|
const headersList = await headers();
|
|
const forwardedFor = headersList.get('x-forwarded-for');
|
|
const realIp = headersList.get('x-real-ip');
|
|
const ip = forwardedFor?.split(',')[0] || realIp || 'unknown';
|
|
const userAgent = headersList.get('user-agent') || 'unknown';
|
|
|
|
try {
|
|
const response = await fetch(`${API_BASE_URL}/api/file/link`, {
|
|
method: 'POST',
|
|
body: JSON.stringify({
|
|
file_id: fileId,
|
|
ip: ip,
|
|
user_agent: userAgent
|
|
}),
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': `Bearer ${token}`
|
|
}
|
|
});
|
|
|
|
if (response.ok) {
|
|
const contentType = response.headers.get('content-type');
|
|
|
|
if (contentType?.includes('application/json')) {
|
|
const data = await response.json();
|
|
return data.message_body;
|
|
} else {
|
|
const blob = await response.blob();
|
|
return URL.createObjectURL(blob);
|
|
}
|
|
} else {
|
|
throw new Error(`${response.status}`);
|
|
}
|
|
} catch (error) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export async function requestFileAsBuffer(fileId: string): Promise<ArrayBuffer | null> {
|
|
const token = await getSessionData('token');
|
|
const headersList = await headers();
|
|
const forwardedFor = headersList.get('x-forwarded-for');
|
|
const realIp = headersList.get('x-real-ip');
|
|
const ip = forwardedFor?.split(',')[0] || realIp || 'unknown';
|
|
const userAgent = headersList.get('user-agent') || 'unknown';
|
|
|
|
try {
|
|
const response = await fetch(`${API_BASE_URL}/api/file/link`, {
|
|
method: 'POST',
|
|
body: JSON.stringify({
|
|
file_id: fileId,
|
|
ip: ip,
|
|
user_agent: userAgent
|
|
}),
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': `Bearer ${token}`
|
|
}
|
|
});
|
|
|
|
if (!response.ok) {
|
|
throw new Error(`${response.status}`);
|
|
}
|
|
|
|
const contentType = response.headers.get('content-type');
|
|
if (contentType?.includes('application/json')) {
|
|
const data = await response.json();
|
|
const fileUrl = data.message_body;
|
|
const fileResponse = await fetch(fileUrl);
|
|
if (!fileResponse.ok) {
|
|
throw new Error(`Failed to fetch file: ${fileResponse.status}`);
|
|
}
|
|
|
|
return await fileResponse.arrayBuffer();
|
|
}
|
|
else {
|
|
return await response.arrayBuffer();
|
|
}
|
|
} catch (error) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export async function fetchDocumentInfo(fileId: string) {
|
|
const token = await getSessionData('token');
|
|
console.log('fetchDocumentInfo');
|
|
|
|
try {
|
|
const response = await fetch(`${API_BASE_URL}/api/files/public/file-info/${fileId}`, {
|
|
method: 'GET',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': `Bearer ${token}`
|
|
}
|
|
});
|
|
|
|
if (response.ok) {
|
|
const parsed = await response.json();
|
|
return parsed;
|
|
} else {
|
|
throw new Error(`${response.status}`);
|
|
}
|
|
} catch (error) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export async function filePermisionChange(fileId: string, permissions: boolean) {
|
|
const token = await getSessionData('token');
|
|
|
|
try {
|
|
const response = await fetch(`${API_BASE_URL}/api/v1/data`, {
|
|
method: 'POST',
|
|
body: JSON.stringify({
|
|
version: 1,
|
|
msg_id: 20005,
|
|
message_body: {
|
|
file_id: fileId,
|
|
action: 'change_permission',
|
|
permissions: {
|
|
DOWNLOAD: permissions
|
|
}
|
|
}
|
|
}),
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': `Bearer ${token}`
|
|
}
|
|
});
|
|
|
|
if (response.ok) {
|
|
const parsed = await response.json();
|
|
|
|
if (parsed.message_code === 0) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
|
|
} else {
|
|
throw new Error(`${response.status}`);
|
|
}
|
|
} catch (error) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export async function fetchHistoryView(
|
|
page: number,
|
|
size: number,
|
|
locale: string,
|
|
sortDirection?: 'asc' | 'desc',
|
|
sortBy?: string
|
|
) {
|
|
const token = await getSessionData('token');
|
|
|
|
try {
|
|
const response = await fetch(`${API_BASE_URL}/api/v1/data`, {
|
|
method: 'POST',
|
|
body: JSON.stringify({
|
|
version: 1,
|
|
msg_id: 30028,
|
|
message_body: {
|
|
action: 'history_view',
|
|
page: page,
|
|
size: size,
|
|
locale: locale,
|
|
sort_direction: sortDirection,
|
|
sort_by: sortBy
|
|
}
|
|
}),
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': `Bearer ${token}`
|
|
}
|
|
});
|
|
|
|
if (response.ok) {
|
|
const parsed = await response.json();
|
|
|
|
if (parsed.message_code === 0) {
|
|
return parsed.message_body;
|
|
} else {
|
|
return null;
|
|
}
|
|
|
|
} else {
|
|
throw new Error(`${response.status}`);
|
|
}
|
|
} catch (error) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export async function fetchTrackingStats() {
|
|
const token = await getSessionData('token');
|
|
|
|
try {
|
|
const response = await fetch(`${API_BASE_URL}/api/v1/data`, {
|
|
method: 'POST',
|
|
body: JSON.stringify({
|
|
version: 1,
|
|
msg_id: 30028,
|
|
message_body: {
|
|
action: 'view_stats'
|
|
}
|
|
}),
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': `Bearer ${token}`
|
|
}
|
|
});
|
|
|
|
if (response.ok) {
|
|
const parsed = await response.json();
|
|
|
|
if (parsed.message_code === 0) {
|
|
return parsed.message_body;
|
|
} else {
|
|
return null;
|
|
}
|
|
|
|
} else {
|
|
throw new Error(`${response.status}`);
|
|
}
|
|
} catch (error) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export async function downloadTrackingFile(fileId: string, fileName?: string) {
|
|
const token = await getSessionData('token');
|
|
|
|
try {
|
|
const response = await fetch(`${API_BASE_URL}/api/v1/files/public-download/${fileId}`, {
|
|
method: 'GET',
|
|
headers: {
|
|
'Authorization': `Bearer ${token}`
|
|
}
|
|
});
|
|
|
|
if (!response.ok) {
|
|
throw 'failed-to-download-file';
|
|
}
|
|
|
|
const arrayBuffer = await response.arrayBuffer()
|
|
const base64 = Buffer.from(arrayBuffer).toString('base64')
|
|
|
|
return {
|
|
data: base64,
|
|
contentType: response.headers.get('content-type') || 'application/octet-stream',
|
|
fileName: fileName ? fileName : 'fileName'
|
|
}
|
|
} catch (error) {
|
|
return null;
|
|
}
|
|
} |