-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.js
100 lines (86 loc) · 2.65 KB
/
server.js
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
/**
* Copyright 2023 Adobe
* All Rights Reserved.
*
* NOTICE: Adobe permits you to use, modify, and distribute this file in
* accordance with the terms of the Adobe license agreement accompanying
* it.
*/
var express = require('express');
const fileUpload = require('express-fileupload');
const cors = require('cors');
const fs = require('fs');
const fsPromises = fs.promises;
const bodyParser = require('body-parser');
const morgan = require('morgan');
const path = require('path');
const _ = require('lodash');
const fetch = require('node-fetch');
const util = require('util');
const child = require('child_process')
let exec = util.promisify(child.exec);
const port = process.env.PORT || 8000;
var app = express();
// serve our web client
app.use(express.static('client'));
// Allow urls from the uploads folder to be served
let imageFolder = 'uploads'
app.use(express.static(imageFolder));
// Create a local folder to hold images in this example.
if(!fs.existsSync(imageFolder)){
fs.mkdirSync(imageFolder)
}
// Enable files upload.
app.use(fileUpload({
createParentPath: true,
limits: {
fileSize: 2 * 1024 * 1024 * 1024 // max upload file(s) size
},
}));
// Add other middleware.
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.raw({type:"image/*",limit:'20mb', extended:true}));
app.use(bodyParser.urlencoded({extended: true}));
app.use(morgan('dev'));
// Runs c2patool to get version info using exec
app.get('/version', async function (req, res) {
try {
let result = await exec('./c2patool --version');
console.log(result);
res.send(result.stdout);
} catch (err) {
res.status(500).send(err);
}
});
// Uploads a file, adds a C2PA manifest and returns a URL
app.post('/upload', async (req, res) => {
try {
let fileName = req.query.name;
let filePath = `${imageFolder}/${fileName}`;
// upload the file
await fsPromises.appendFile(filePath, Buffer.from(req.body),{flag:'w'});
// call c2patool to add a manifest
let command = `./c2patool "${filePath}" -m manifest.json -o "${filePath}" -f`;
let result = await exec(command);
// get the manifest store report from stdout
let report = JSON.parse(result.stdout)
res.send({
name: fileName,
url: `http://localhost:${port}/${fileName}`,
report
});
} catch (err) {
console.log(err);
// return errors to the client
res.status(500).send(err);
}
});
// the default endpoint is test page for this service
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname, 'client/index.html'));
});
// start the http server
app.listen(port, () =>
console.log(`CAI HTTP server listening on port ${port}.`)
);