Split nameAndExtension to seperate variables
This commit is contained in:
parent
02734f1d9d
commit
234791cd9a
5 changed files with 40 additions and 40 deletions
|
@ -6,7 +6,6 @@ import process from "process";
|
||||||
import { extension, videoExtensions, imageExtensions, oembedObj } from "./lib";
|
import { extension, videoExtensions, imageExtensions, oembedObj } from "./lib";
|
||||||
import { insertToDB } from "./db";
|
import { insertToDB } from "./db";
|
||||||
import { ffmpegDownscale, ffProbe } from "./ffmpeg";
|
import { ffmpegDownscale, ffProbe } from "./ffmpeg";
|
||||||
import { ffprobe } from "fluent-ffmpeg";
|
|
||||||
|
|
||||||
export const checkAuth: Middleware = (req, res, next) => {
|
export const checkAuth: Middleware = (req, res, next) => {
|
||||||
if (!req.user) {
|
if (!req.user) {
|
||||||
|
@ -49,8 +48,8 @@ export const checkSharexAuth: Middleware = (req, res, next) => {
|
||||||
export const createEmbedData: Middleware = async (req, res, next) => {
|
export const createEmbedData: Middleware = async (req, res, next) => {
|
||||||
const files = req.files as Express.Multer.File[];
|
const files = req.files as Express.Multer.File[];
|
||||||
for (const file in files) {
|
for (const file in files) {
|
||||||
const nameAndExtension = extension(files[file].filename);
|
const [filename, fileExtension] = extension(files[file].filename);
|
||||||
const isMedia = videoExtensions.includes(nameAndExtension[1]) || imageExtensions.includes(nameAndExtension[1]);
|
const isMedia = videoExtensions.includes(fileExtension) || imageExtensions.includes(fileExtension);
|
||||||
|
|
||||||
const oembed: oembedObj = {
|
const oembed: oembedObj = {
|
||||||
type: "video",
|
type: "video",
|
||||||
|
@ -59,19 +58,18 @@ export const createEmbedData: Middleware = async (req, res, next) => {
|
||||||
provider_url: "https://github.com/WaveringAna/embedder",
|
provider_url: "https://github.com/WaveringAna/embedder",
|
||||||
cache_age: 86400,
|
cache_age: 86400,
|
||||||
html: `<iframe src='${req.protocol}://${req.get("host")}/gifv/${
|
html: `<iframe src='${req.protocol}://${req.get("host")}/gifv/${
|
||||||
nameAndExtension[0]
|
filename
|
||||||
}${nameAndExtension[1]}'></iframe>`
|
}${fileExtension}'></iframe>`
|
||||||
};
|
};
|
||||||
|
|
||||||
if (isMedia) {
|
if (isMedia) {
|
||||||
let ffProbeData;
|
let ffProbeData;
|
||||||
try { ffProbeData = await ffProbe(
|
try { ffProbeData = await ffProbe(
|
||||||
`uploads/${files[file].filename}`,
|
`uploads/${files[file].filename}`,
|
||||||
nameAndExtension[0],
|
filename,
|
||||||
nameAndExtension[1],
|
fileExtension,
|
||||||
); } catch (error) {
|
); } catch (error) {
|
||||||
console.log(`Error: ${error}`);
|
console.log(`Error: ${error}`);
|
||||||
console.log(nameAndExtension[1]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
oembed.width = ffProbeData.streams[0].width;
|
oembed.width = ffProbeData.streams[0].width;
|
||||||
|
@ -79,12 +77,12 @@ export const createEmbedData: Middleware = async (req, res, next) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
fs.writeFile(
|
fs.writeFile(
|
||||||
`uploads/oembed-${nameAndExtension[0]}${nameAndExtension[1]}.json`,
|
`uploads/oembed-${filename}${fileExtension}.json`,
|
||||||
JSON.stringify(oembed),
|
JSON.stringify(oembed),
|
||||||
function (err) {
|
function (err) {
|
||||||
if (err) return next(err);
|
if (err) return next(err);
|
||||||
console.log(
|
console.log(
|
||||||
`oembed file created ${nameAndExtension[0]}${nameAndExtension[1]}.json`,
|
`oembed file created ${filename}${fileExtension}.json`,
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
@ -97,24 +95,24 @@ export const convertTo720p: Middleware = (req, res, next) => {
|
||||||
const files = req.files as Express.Multer.File[];
|
const files = req.files as Express.Multer.File[];
|
||||||
console.log("convert to 720p running");
|
console.log("convert to 720p running");
|
||||||
for (const file in files) {
|
for (const file in files) {
|
||||||
const nameAndExtension = extension(files[file].filename);
|
const [filename, fileExtension] = extension(files[file].filename);
|
||||||
|
|
||||||
//Skip if not a video
|
//Skip if not a video
|
||||||
if (
|
if (
|
||||||
!videoExtensions.includes(nameAndExtension[1]) &&
|
!videoExtensions.includes(fileExtension) &&
|
||||||
nameAndExtension[1] !== ".gif"
|
fileExtension !== ".gif"
|
||||||
) {
|
) {
|
||||||
console.log(`${files[file].filename} is not a video file`);
|
console.log(`${files[file].filename} is not a video file`);
|
||||||
console.log(nameAndExtension[1]);
|
console.log(fileExtension);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(`Creating 720p for ${files[file].filename}`);
|
console.log(`Creating 720p for ${files[file].filename}`);
|
||||||
|
|
||||||
ffmpegDownscale(
|
ffmpegDownscale(
|
||||||
`uploads/${nameAndExtension[0]}${nameAndExtension[1]}`,
|
`uploads/${filename}${fileExtension}`,
|
||||||
nameAndExtension[0],
|
filename,
|
||||||
nameAndExtension[1],
|
fileExtension,
|
||||||
)
|
)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
//Nothing for now, can fire event flag that it is done to front end when react conversion is done
|
//Nothing for now, can fire event flag that it is done to front end when react conversion is done
|
||||||
|
|
|
@ -23,11 +23,11 @@ export const fileStorage = multer.diskStorage({
|
||||||
file: Express.Multer.File,
|
file: Express.Multer.File,
|
||||||
callback: FileNameCallback,
|
callback: FileNameCallback,
|
||||||
): void => {
|
): void => {
|
||||||
const nameAndExtension = extension(file.originalname);
|
const [filename, fileExtension] = extension(file.originalname);
|
||||||
console.log(`Uploading ${file}`);
|
console.log(`Uploading ${file}`);
|
||||||
db.all(
|
db.all(
|
||||||
"SELECT * FROM media WHERE path = ?",
|
"SELECT * FROM media WHERE path = ?",
|
||||||
[nameAndExtension[0] + nameAndExtension[1]],
|
[filename + fileExtension],
|
||||||
(err: Error, exists: []) => {
|
(err: Error, exists: []) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.log(err);
|
console.log(err);
|
||||||
|
@ -43,12 +43,12 @@ export const fileStorage = multer.diskStorage({
|
||||||
) {
|
) {
|
||||||
callback(
|
callback(
|
||||||
null,
|
null,
|
||||||
nameAndExtension[0] + "-" + suffix + nameAndExtension[1],
|
filename + "-" + suffix + fileExtension,
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
callback(
|
callback(
|
||||||
null,
|
null,
|
||||||
request.body.title + "-" + suffix + nameAndExtension[1],
|
request.body.title + "-" + suffix + fileExtension,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -57,9 +57,9 @@ export const fileStorage = multer.diskStorage({
|
||||||
request.body.title == null ||
|
request.body.title == null ||
|
||||||
request.body.title == undefined
|
request.body.title == undefined
|
||||||
) {
|
) {
|
||||||
callback(null, nameAndExtension[0] + nameAndExtension[1]);
|
callback(null, filename + fileExtension);
|
||||||
} else {
|
} else {
|
||||||
callback(null, request.body.title + nameAndExtension[1]);
|
callback(null, request.body.title + fileExtension);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
@ -255,8 +255,8 @@ function checkFileAvailability(filePath) {
|
||||||
function createVideoElement(filePath) {
|
function createVideoElement(filePath) {
|
||||||
const videoContainer = document.getElementById(`video-${filePath}`);
|
const videoContainer = document.getElementById(`video-${filePath}`);
|
||||||
videoContainer.outerHTML = `
|
videoContainer.outerHTML = `
|
||||||
<video id='video-${filePath}' class="image" autoplay loop muted playsinline loading="lazy">
|
<video id='video-${filePath}' class="image" autoplay loop muted playsinline>
|
||||||
<source src="/uploads/720p-${filePath}" loading="lazy">
|
<source src="/uploads/720p-${filePath}">
|
||||||
</video>
|
</video>
|
||||||
`;
|
`;
|
||||||
videoContainer.style.display = "block";
|
videoContainer.style.display = "block";
|
||||||
|
|
|
@ -69,7 +69,7 @@ router.get(
|
||||||
);
|
);
|
||||||
|
|
||||||
router.get("/media-list", fetchMedia, (req: Request, res: Response) => {
|
router.get("/media-list", fetchMedia, (req: Request, res: Response) => {
|
||||||
res.render("partials/_fileList", { user: req.user }); // Render only the file list partial
|
res.render("partials/_fileList", { user: req.user });
|
||||||
});
|
});
|
||||||
|
|
||||||
router.get(
|
router.get(
|
||||||
|
@ -81,17 +81,17 @@ router.get(
|
||||||
let width;
|
let width;
|
||||||
let height;
|
let height;
|
||||||
|
|
||||||
const nameAndExtension = extension(`uploads/${req.params.file}`);
|
const [filename, fileExtension] = extension(`uploads/${req.params.file}`);
|
||||||
if (
|
if (
|
||||||
nameAndExtension[1] == ".mp4" ||
|
fileExtension == ".mp4" ||
|
||||||
nameAndExtension[1] == ".mov" ||
|
fileExtension == ".mov" ||
|
||||||
nameAndExtension[1] == ".webm" ||
|
fileExtension == ".webm" ||
|
||||||
nameAndExtension[1] == ".gif"
|
fileExtension == ".gif"
|
||||||
) {
|
) {
|
||||||
const imageData = ffProbe(
|
const imageData = ffProbe(
|
||||||
`uploads/${req.params.file}`,
|
`uploads/${req.params.file}`,
|
||||||
nameAndExtension[0],
|
filename,
|
||||||
nameAndExtension[1]
|
fileExtension
|
||||||
);
|
);
|
||||||
|
|
||||||
width = (await imageData).streams[0].width;
|
width = (await imageData).streams[0].width;
|
||||||
|
@ -147,18 +147,17 @@ router.get(
|
||||||
[checkAuth],
|
[checkAuth],
|
||||||
async (req: Request, res: Response, next: NextFunction) => {
|
async (req: Request, res: Response, next: NextFunction) => {
|
||||||
const filename: any = await getPath(req.params.id);
|
const filename: any = await getPath(req.params.id);
|
||||||
console.log(filename);
|
|
||||||
const filePath = path.join(__dirname , "../../uploads/" + filename.path);
|
const filePath = path.join(__dirname , "../../uploads/" + filename.path);
|
||||||
const oembed = path.join(
|
const oembed = path.join(
|
||||||
__dirname , "../../uploads/oembed-" + filename.path + ".json"
|
__dirname , "../../uploads/oembed-" + filename.path + ".json"
|
||||||
);
|
);
|
||||||
|
|
||||||
const nameAndExtension = extension(filePath);
|
const [fileName, fileExtension] = extension(filePath);
|
||||||
const filesToDelete = [filePath, oembed];
|
const filesToDelete = [filePath, oembed];
|
||||||
|
|
||||||
if (
|
if (
|
||||||
videoExtensions.includes(nameAndExtension[1]) ||
|
videoExtensions.includes(fileExtension) ||
|
||||||
nameAndExtension[1] == ".gif"
|
fileExtension == ".gif"
|
||||||
) {
|
) {
|
||||||
filesToDelete.push(
|
filesToDelete.push(
|
||||||
path.join(__dirname , "../../uploads/720p-" + filename.path)
|
path.join(__dirname , "../../uploads/720p-" + filename.path)
|
||||||
|
@ -186,7 +185,10 @@ router.get(
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
);
|
).catch((err) => {
|
||||||
|
console.error("Error deleting files:", err);
|
||||||
|
return next(err);
|
||||||
|
});
|
||||||
|
|
||||||
next();
|
next();
|
||||||
},
|
},
|
||||||
|
|
|
@ -23,8 +23,8 @@ const imageExtensions = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.svg', '.tiff
|
||||||
|
|
||||||
<!-- Hidden video container to be displayed later -->
|
<!-- Hidden video container to be displayed later -->
|
||||||
<div class="video">
|
<div class="video">
|
||||||
<video id="video-<%= file.path %>" class="image" autoplay loop muted playsinline loading="lazy" style="display: none;">
|
<video id="video-<%= file.path %>" class="image" autoplay loop muted playsinline style="display: none;">
|
||||||
<source src="/uploads/720p-<%= file.path %>" loading="lazy">
|
<source src="/uploads/720p-<%= file.path %>">
|
||||||
</video>
|
</video>
|
||||||
<div class="overlay">
|
<div class="overlay">
|
||||||
<% if(user.username == "admin" && file.username != "admin") { %>
|
<% if(user.username == "admin" && file.username != "admin") { %>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue