Skip to content

Commit

Permalink
feat(Filesystem): Replace walk with filesystem, Next doesn't use __di…
Browse files Browse the repository at this point in the history
…rname
  • Loading branch information
danactive committed May 8, 2021
1 parent 7ede8f9 commit a355a72
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 145 deletions.
1 change: 0 additions & 1 deletion api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
"hapi-swagger": "^13.1.0",
"isomorphic-fetch": "^2.2.1",
"jquery-colorbox": "^1.6.4",
"mime-types": "^2.1.28",
"node-notifier": "^9.0.0",
"react": "^16.14.0",
"react-dom": "^16.14.0",
Expand Down
93 changes: 1 addition & 92 deletions api/server/plugins/utils/lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
const appRoot = require('app-root-path');
const boom = require('boom');
const dotenv = require('dotenv');
const dotProp = require('dot-prop');
const glob = require('glob');
const mime = require('mime-types');
const path = require('path');

const configFile = require('../../../../../config.json');
Expand Down Expand Up @@ -35,69 +33,8 @@ function clone(obj) {
return JSON.parse(JSON.stringify(obj));
}

function customMime(rawExtension) {
const extension = (rawExtension) ? rawExtension.toLowerCase() : null;

if (['raw', 'arw'].includes(extension)) {
return 'image/raw';
}

if (['m2ts', 'mts'].includes(extension)) {
return 'video/mp2t';
}

const photoTypes = configFile.supportedFileTypes.photo.concat(configFile.rawFileTypes.photo);
if (photoTypes.includes(extension)) {
return 'image';
}

const videoTypes = configFile.supportedFileTypes.video.concat(configFile.rawFileTypes.video);
if (videoTypes.includes(extension)) {
return 'video';
}

return false;
}

const fileMethods = {
type: (filepath) => {
if (!filepath) {
return false;
}

if (filepath.lastIndexOf('.') === 0) {
return path.parse(filepath).name.substr(1);
}

return path.extname(filepath).substr(1);
},
mimeType: (extension) => customMime(extension) || mime.lookup(extension),
mediumType: (extension) => {
if (!extension) {
return false;
}

if (typeof extension !== 'string') {
return false;
}

if (extension.indexOf('/') === -1) {
if (['image', 'photo'].includes(extension)) {
return 'image';
}

if (['video'].includes(extension)) {
return 'video';
}

return false;
}

return extension.split('/')[0];
},
absolutePath: (filepath) => (path.isAbsolute(filepath) ? filepath : appRoot.resolve(filepath)),
photoPath: (filepath) => filepath && filepath.replace('thumbs', 'photos'),
};

fileMethods.videoToThumbsPath = (filepath = null, gallery = null) => {
if (filepath === null || gallery === null) {
Expand Down Expand Up @@ -138,34 +75,6 @@ fileMethods.glob = (sourceFolder, pattern, options = {}) => new Promise((resolve
});
});

/*
Construct a file system path from the history public folder
@method safePublicPath
@param {string} relative or absolute path from /history/public folder; root absolute paths are rejected
@return {Promise} string
*/
fileMethods.safePublicPath = (rawDestinationPath) => {
try {
const normalizedDestinationPath = path.normalize(rawDestinationPath);
const publicPath = path.normalize(path.join(__dirname, '../../../../../public'));
const isRawInPublic = normalizedDestinationPath.startsWith(publicPath);
const safeDestinationPath = (isRawInPublic) ? normalizedDestinationPath : path.join(publicPath, normalizedDestinationPath);

if (!safeDestinationPath.startsWith(publicPath)) {
throw boom.forbidden(`Restrict to public file system (${safeDestinationPath}); publicPath(${publicPath})`);
}

return safeDestinationPath;
} catch (error) {
if (error.name === 'TypeError') { // path core module error
throw boom.notAcceptable('Invalid file system path');
}

throw boom.boomify(error);
}
};

module.exports = {
env, config, clone, file: fileMethods,
env, config, clone
};
37 changes: 0 additions & 37 deletions api/server/plugins/walk/lib/files.js

This file was deleted.

38 changes: 23 additions & 15 deletions app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
"test:ci": "jest --ci"
},
"dependencies": {
"app-root-path": "^3.0.0",
"boom": "^7.0.0",
"glob": "^7.1.6",
"mime-types": "^2.1.28",
"next": "^10.0.0",
"react": "17.0.1",
"react-dom": "17.0.1"
Expand Down
14 changes: 14 additions & 0 deletions app/pages/api/admin/filesystem/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import local from './local'

const errorSchema = (message) => ({ galleries: [], error: { message } })

export default async function handler(req, res) {
switch (req.method) {
case 'GET': {
const out = await local.get()
return res.status(out.status).json(out.body)
}
default:
return res.status(405).json(errorSchema(`Method ${req.method} Not Allowed`))
}
}
37 changes: 37 additions & 0 deletions app/pages/api/admin/filesystem/local.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import globCallback from 'glob'
import path from 'path'
import { promisify } from 'util'

import utils from './utils'

const glob = promisify(globCallback)

async function get(destPath = '') {
const publicPath = utils.safePublicPath('/')
const globPath = path.join(publicPath, destPath)

if (!globPath.startsWith(publicPath)) {
throw new URIError('Invalid system path')
}

const files = await glob(decodeURI(`${globPath}/*`))

const webPaths = files.map((file) => {
const fileMeta = {}
fileMeta.ext = utils.type(file) // case-insensitive
fileMeta.name = path.basename(file, `.${fileMeta.ext}`)
fileMeta.filename = (fileMeta.ext === '') ? fileMeta.name : `${fileMeta.name}.${fileMeta.ext}`
fileMeta.path = file.replace(globPath, destPath)

const mediumType = utils.mediumType(utils.mimeType(fileMeta.ext))
fileMeta.mediumType = mediumType || 'folder'

return fileMeta
})

return { body: { files: webPaths, destPath }, status: 200 }
}

export default {
get,
}
Loading

0 comments on commit a355a72

Please sign in to comment.