Skip to content
This repository has been archived by the owner on May 20, 2022. It is now read-only.

Commit

Permalink
Merge pull request #29 from izu-co/worker-update
Browse files Browse the repository at this point in the history
Worker update
  • Loading branch information
izu-co authored Sep 16, 2021
2 parents 9f553fa + 3aed8f2 commit 9ebbef4
Show file tree
Hide file tree
Showing 30 changed files with 7,720 additions and 7,596 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dexie.js
2 changes: 2 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
"@typescript-eslint/ban-types": "off",
"@typescript-eslint/no-explicit-any": "off",
"no-async-promise-executor": "off",
"no-constant-condition": "off",
"no-case-declarations": "off",
"indent": [
"error",
4
Expand Down
33 changes: 20 additions & 13 deletions backend/createImages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { PassThrough } from 'stream';
*/
async function createImages(path:string, override:boolean, verbose = false, maxConcurrent = 25): Promise<BackendRequest<FileData[]>> {
console.log('[INFO] Started Image generation');
const t = Date.now();
const paths = (await getAllFiles(path)).filter(file => VideoNameExtensions.includes(file.path.split('.').pop()) || file.stats.isDirectory());

const createdImages = [];
Expand All @@ -36,9 +37,8 @@ async function createImages(path:string, override:boolean, verbose = false, maxC
while (files.length > 0) {
for (let i = 0; i < Math.min(files.length, maxConcurrent); i++) {
filesPromises.push(new Promise(async (resolve) => {
const start = Date.now();
const file = files[i];
if (verbose)
console.log(`[INFO] Started ${file.path}`);
const exists = await new Promise(resolve => {
fs.promises.access(file.path + '.jpg', fs.constants.F_OK)
.then(() => resolve(true))
Expand All @@ -47,7 +47,7 @@ async function createImages(path:string, override:boolean, verbose = false, maxC
if (!exists || override) {
const ok = await generateImage(file.path);
if (ok && verbose)
console.log(`[INFO] Finished ${file.path}.jpg`);
console.log(`[INFO] Finished ${file.path}.jpg (${(Date.now() - start) / 1000})`);
else if (!ok)
console.log(`[ERROR] Unable to create image ${file.path}.jpg`);
if (ok)
Expand All @@ -69,9 +69,8 @@ async function createImages(path:string, override:boolean, verbose = false, maxC
while (folders.length > 0) {
for (let i = 0; i < Math.min(folders.length, maxConcurrent); i++) {
folderPromises.push(new Promise(async (resolve) => {
const start = Date.now();
const file = folders[i];
if (verbose)
console.log(`[INFO] Started ${file.path}`);
const exists = await new Promise(resolve => {
fs.promises.access(file.path + '.jpg', fs.constants.F_OK)
.then(() => resolve(true))
Expand All @@ -80,7 +79,7 @@ async function createImages(path:string, override:boolean, verbose = false, maxC
if (!exists || override) {
generateFolderImage(file.path).then(ok => {
if (ok && verbose)
console.log(`[INFO] Finished ${file.path}.jpg`);
console.log(`[INFO] Finished ${file.path}.jpg (${(Date.now() - start) / 1000})`);
else if (!ok)
console.log(`[ERROR] Unable to create image ${file.path}.jpg`);
if (ok)
Expand All @@ -102,6 +101,7 @@ async function createImages(path:string, override:boolean, verbose = false, maxC
console.log('[INFO] Image generation done');
appEvents.emit('finished', 'image generation');
clearInterval(updateIntervall);
console.log(Date.now() - t);
return {
isOk: true,
value: createdImages
Expand All @@ -121,13 +121,13 @@ const generateFolderImage = async (path:string): Promise<boolean> => {
await fs.promises.copyFile(filteredFiles[0].path, path + '.jpg');
return true;
case 2:
await (await combine2(filteredFiles[0].path, filteredFiles[1].path)).writeAsync(path + '.jpg');
await saveImage(await combine2(filteredFiles[0].path, filteredFiles[1].path), path + '.jpg');
return true;
case 3:
await (await combine3(filteredFiles[0].path, filteredFiles[1].path, filteredFiles[2].path)).writeAsync(path + '.jpg');
await saveImage(await combine3(filteredFiles[0].path, filteredFiles[1].path, filteredFiles[2].path), path + '.jpg');
return true;
default:
await (await combine4(...filteredFiles.map(a => a.path))).writeAsync(path + '.jpg');
await saveImage(await combine4(...filteredFiles.map(a => a.path)), path + '.jpg');
return true;
}
};
Expand Down Expand Up @@ -208,13 +208,13 @@ const generateImage = async (path: string) : Promise<boolean> => {
if (!lastImage)
return false;

await lastImage.writeAsync(path + '.jpg');
await saveImage(lastImage, path + '.jpg');
return true;
} else {
if (!img.hasMore) {
if (!lastImage)
return false;
await lastImage.writeAsync(path + '.jpg');
await saveImage(lastImage, path + '.jpg');
return true;
} else if (img.imageData.length > 0) {
lastImage = await readImage(img.imageData);
Expand All @@ -234,13 +234,13 @@ const generateImage = async (path: string) : Promise<boolean> => {
}
}
if ((blackPixel / pixelAmount) <= 0.8 && (whitePixel / pixelAmount) <= 0.8) {
await lastImage.writeAsync(path + '.jpg');
await saveImage(lastImage, path + '.jpg');
return true;
}
} else {
if (!lastImage)
return false;
await lastImage.writeAsync(path + '.jpg');
await saveImage(lastImage, path + '.jpg');
return true;
}
}
Expand Down Expand Up @@ -270,6 +270,7 @@ const extractImage = async (path: string, percent: number) : Promise<ImageAnswer
.addOption('-frames:v 1')
.format('mjpeg')
.seekInput(time)
.addOption('-skip_frame nokey')
.on('error', (er, stdout, stderr) => {
if (er.message === 'Output stream closed' )
return;
Expand Down Expand Up @@ -387,6 +388,12 @@ const readImageFromFile = (data: string) => {
});
};

const saveImage = async (jimp: jimp, path: string) => {
const buffer = await jimp.getBufferAsync(jimp.getMIME());
await fs.promises.writeFile(path, buffer);
return true;
};

const readdir = promisify(fs.readdir);
const stat = promisify(fs.stat);

Expand Down
45 changes: 0 additions & 45 deletions backend/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
import {
join
} from 'path';
import * as chok from 'chokidar';

if (!fs.existsSync(path.join(__dirname, '..', 'data')))
fs.mkdirSync(path.join(__dirname, '..', 'data'));
Expand Down Expand Up @@ -45,50 +44,6 @@ getAllFiles(argv['Video Directory']).forEach(file => {
fileIndex.prepare('INSERT INTO files VALUES(?,?,?)').run(file, fs.statSync(file).mtimeMs, fs.statSync(file).isDirectory() ? 1 : 0);
});

chok.watch(argv['Video Directory'], {
alwaysStat: true,
persistent: false,
ignored: null,
atomic: 100
}).on('add', (path, stats) => {
if (path.substring(path.lastIndexOf('.') + 1) == 'jpg' && fs.existsSync(path.substring(0, path.lastIndexOf('.')))) {
if (fileIndex.prepare('SELECT * FROM files WHERE path=?').get(path.substring(0, path.lastIndexOf('.'))) == null) {
fileIndex.prepare('INSERT INTO files VALUES(?,?,?)').run(path.substring(0, path.lastIndexOf('.')), fs.lstatSync(path.substring(0, path.lastIndexOf('.'))).mtimeMs, fs.lstatSync(path.substring(0, path.lastIndexOf('.'))).isDirectory() ? 1 : 0);
}
} else {
if (VideoNameExtensions.includes(path.substring(path.lastIndexOf('.') + 1)) &&
fs.existsSync(path + '.jpg')) {
if (fileIndex.prepare('SELECT * FROM files WHERE path=?').get(path) == null) {
fileIndex.prepare('INSERT INTO files VALUES(?,?,?)').run(path, stats ? stats.mtimeMs : 0, 0);
}
}
}
}).on('unlink', (path) => {
if (path.substring(path.lastIndexOf('.') + 1) == 'jpg') {
if (fileIndex.prepare('SELECT * FROM files WHERE path=?').get(path.substring(0, path.lastIndexOf('.'))) != null) {
fileIndex.prepare('DELETE FROM files WHERE path=?').run(path.substring(0, path.lastIndexOf('.')));
}
} else {
if (VideoNameExtensions.includes(path.substring(path.lastIndexOf('.') + 1))) {
if (fileIndex.prepare('SELECT * FROM files WHERE path=?').get(path) != null) {
fileIndex.prepare('DELETE FROM files WHERE path=?').run(path);
}
}
}
}).on('addDir', (path, stats) => {
if (fs.existsSync(path + '.jpg')) {
if (fileIndex.prepare('SELECT * FROM files WHERE path=?').get(path) == null) {
fileIndex.prepare('INSERT INTO files VALUES(?,?,?)').run(path, stats ? stats.mtimeMs : 0, 1);
}
}
}).on('unlinkDir', (path) => {
if (fileIndex.prepare('SELECT * FROM files WHERE path=?').get(path) != null)
fileIndex.prepare('DELETE FROM files WHERE path=?').run(path);
}).on('change', (path, stats) => {
if (stats && fileIndex.prepare('SELECT * FROM files WHERE path=?').get(path) != null)
fileIndex.prepare('UPDATE files SET created=? WHERE path=?').run(stats.mtimeMs, path);
});

console.log('[INFO] Finished indexing!');

db.exec('CREATE TABLE IF NOT EXISTS properties (name STRING, val TEXT)');
Expand Down
4 changes: 2 additions & 2 deletions backend/fileStuff.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { createImages } from './videoServing/createImages';
import { loadTime } from './videoServing/loadTime';
import { getFiles, getFileAmount } from './videoServing/getFiles';
import { getFiles, getFileAmount, getMetaData } from './videoServing/getFiles';
import { saveTime } from './videoServing/saveTime';
import { getFileData } from './videoServing/getFileData';
import { getSortTypes } from './videoServing/getSortTypes';
import { addToWatchList, IsOnWatchList, removeFromWatchList } from './videoServing/watchlist';
import { getStars, setStars } from './videoServing/stars';

export { createImages, loadTime, getFiles, saveTime, getFileData, getSortTypes, addToWatchList, IsOnWatchList, removeFromWatchList, getStars, setStars, getFileAmount };
export { createImages, loadTime, getFiles, saveTime, getFileData, getSortTypes, addToWatchList, IsOnWatchList, removeFromWatchList, getStars, setStars, getFileAmount, getMetaData };
42 changes: 40 additions & 2 deletions backend/videoServing/getFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const getFileAmount = async (path:string, token:string, ip:string, type:null|str
if (pathCheck.isOk === false)
return pathCheck;
path = pathCheck.value;
if(!fs.existsSync(path) || !fs.lstatSync(path).isDirectory()) return {
if(!(fs.existsSync(path) || !fs.lstatSync(path).isDirectory()) && searchType !== SortTypes.WatchList) return {
isOk: false,
statusCode: 404,
message: 'The given path does not exists'
Expand All @@ -87,6 +87,7 @@ const getFileAmount = async (path:string, token:string, ip:string, type:null|str
const pathCheck = checkPath(path);
if (pathCheck.isOk === false)
return false;
path = pathCheck.value;
if (!fs.existsSync(path))
return false;
if (fs.lstatSync(path).isFile())
Expand Down Expand Up @@ -250,8 +251,45 @@ async function getFileFromCreated(path:string, token:string, ip:string, length:n
};
}

const getMetaData = async (file: string, token: string, ip: string) : Promise<BackendRequest<FileData>> => {
const user = getUserFromToken(token, ip);

if (user.isOk === false) return user;
const pathCheck = checkPath(file);
if (pathCheck.isOk === false) return pathCheck;

const path = pathCheck.value;
if (!fs.existsSync(path)) return { isOk: false, statusCode: 404 };
if (fs.lstatSync(path).isFile())
if (!index.VideoNameExtensions.includes(path.split('.')[path.split('.').length - 1]))
return;
if (!fs.existsSync(path + '.jpg'))
return;
const name = path.substring(path.lastIndexOf(Path.sep));
const stars = getStars(token, ip, path);
const watchlist = IsOnWatchList(user.value, path.replace(index.argv['Video Directory'], ''));
const ret = {
'name' : name,
'Path' : path.replace(index.argv['Video Directory'], ''),
'type' : (fs.lstatSync(path).isDirectory() ? 'folder' : 'video') as 'folder'|'video',
'image' : (await fs.promises.readFile(path + '.jpg')).toString('base64'),
'watchList': watchlist.isOk ? true : false,
'stars': stars.isOk ? stars.value : 0
};
if (ret['type'] === 'video') {
const time = loadTime(pathCheck.value, token, ip, user);
ret['timeStemp'] = time.isOk ? time.value : 0;
}

return {
isOk: true,
value: ret
};

};

function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}

export { getFiles, getFileAmount };
export { getFiles, getFileAmount, getMetaData };
10 changes: 0 additions & 10 deletions html/admin/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,4 @@
<script src="<%= PREFIX_URL %>/js/generalFunctions.js" defer></script>
<script type="text/javascript" src="<%= PREFIX_URL %>/js/admin.js" defer></script>
</body>
<div id="offline" class="false">
<b>
<div>
You are currently
</div>
<div class="important">
OFFLINE
</div>
</b>
</div>
</html>
Loading

0 comments on commit 9ebbef4

Please sign in to comment.