-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
135 lines (105 loc) · 4.45 KB
/
server.js
File metadata and controls
135 lines (105 loc) · 4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import express from 'express';
import axios from 'axios';
import https from 'https';
import 'dotenv/config';
const app = express();
const PORT = 3000;
// Function to retrieve access token
async function getAccessToken() {
const clientId = process.env.CLIENT_ID;
const clientSecret = process.env.CLIENT_SECRET;
const tenantId = process.env.TENANT_ID;
const tokenEndpoint = `https://login.microsoftonline.com/${tenantId}/oauth2/v2.0/token`;
try {
const response = await axios.post(tokenEndpoint, new URLSearchParams({
'client_id': clientId,
'scope': 'https://graph.microsoft.com/.default',
'client_secret': clientSecret,
'grant_type': 'client_credentials'
}), {
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
});
return response.data.access_token;
} catch (error) {
console.error('Error retrieving access token:', error);
throw new Error('Failed to retrieve access token');
}
}
function getFileDetails(inputfileName) {
if (inputfileName.includes('.docx')) {
return {
contentType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
};
}
else if (inputfileName.includes('.mp4')) {
return {
contentType: 'video/mp4'
};
}
else if (inputfileName.includes('.pdf')) {
return {
contentType: 'application/pdf'
};
}
else if (inputfileName.includes('.xlsx')) {
return {
contentType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
};
}
//Default or error if
return {
contentType: 'text/plain'
};
}
app.get('/GraphDownload-doc', async (req, res) => {
const endpointUrl = `https://graph.microsoft.com/v1.0/drives/${process.env.DRIVE_ID}/items/${process.env.ITEM_ID}?select=name,@microsoft.graph.downloadUrl`
try {
const accessToken = await getAccessToken();
//console.log('Retrieved Access Token:', accessToken);
// Make the first GET request to obtain the downloadURL
const response = await axios.get(endpointUrl, {
headers: { 'Authorization': `Bearer ${accessToken}` }
});
// Extract @microsoft.graph.downloadUrl from the response
const downloadUrl = response.data['@microsoft.graph.downloadUrl']; // Use bracket notation to access the property
if (!downloadUrl) {
throw new Error('Download URL not found in response');
}
const fileName = response.data['name'];
const contentType = getFileDetails(fileName).contentType;
// Make the second request to download the file using the obtained downloadUrl
const downloadResponse = await axios.get(downloadUrl, { responseType: 'stream' });
// Optional: Set filename and content type in the response header
res.setHeader('Content-Disposition', `attachment; filename=${fileName}`); // Example filename
res.setHeader('Content-Type', `${contentType}`);
// Stream the download directly to the client
downloadResponse.data.pipe(res);
} catch (error) {
console.error('Error processing download:', error);
res.status(500).send('Failed to process download.');
}
});
app.get('/DirectDownload-doc', async (req, res) => {
const endpointUrl = `https://${process.env.TENANT_NAME}.sharepoint.com/sites/ModernTeam/_layouts/15/download.aspx?UniqueId=524d40f3-6d52-4376-abe4-130f678d05b1`
try {
// const accessToken = await getAccessToken();
const accessToken = process.env.ACESSS_TOKEN;
const fileName = 'sample.mp4';
const contentType = 'video/mp4';
const downloadResponse = await axios.get(endpointUrl, {
responseType: 'stream',
headers: { 'Authorization': `Bearer ${accessToken}` }
});
// Optional: Set filename and content type in the response header
res.setHeader('Content-Disposition', `attachment; filename=${fileName}`); // Example filename
res.setHeader('Content-Type', `${contentType}`);
// Stream the download directly to the client
downloadResponse.data.pipe(res);
} catch (error) {
console.error('Error processing download:', error);
res.status(500).send('Failed to process download.');
}
});
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});