Skip to content

Commit

Permalink
fix: prevent 404 errors in store when using nginx (#1060)
Browse files Browse the repository at this point in the history
  • Loading branch information
robertsLando authored Apr 15, 2021
1 parent 07ffd40 commit b8dd575
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 51 deletions.
80 changes: 34 additions & 46 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ async function startServer (host, port) {
* @returns {string} The path is it's safe, thorws otherwise
*/
function getSafePath (req) {
let reqPath = req.params.path
let reqPath = req.query.path

if (typeof reqPath !== 'string') {
throw Error('Invalid path')
Expand Down Expand Up @@ -830,65 +830,53 @@ app.post('/api/importConfig', apisLimiter, isAuthenticated, async function (
}
})

// get config
// if no path provided return all store dir files/folders, otherwise return the file content
app.get('/api/store', storeLimiter, isAuthenticated, async function (req, res) {
try {
async function parseDir (dir) {
const toReturn = []
const files = await fs.readdir(dir)
for (const file of files) {
const entry = {
name: path.basename(file),
path: utils.joinPath(dir, file)
}
const stats = await fs.lstat(entry.path)
if (stats.isDirectory()) {
entry.children = await parseDir(entry.path)
} else {
entry.ext = file.split('.').pop()
}

entry.size = utils.humanSize(stats.size)
toReturn.push(entry)
}
return toReturn
}
let data
if (req.query.path) {
const reqPath = getSafePath(req)

const data = await parseDir(storeDir)
const stat = await fs.lstat(reqPath)

res.json({ success: true, data: data })
} catch (error) {
logger.error(error.message)
return res.json({ success: false, message: error.message })
}
})

app.get('/api/store/:path', storeLimiter, isAuthenticated, async function (
req,
res
) {
try {
const reqPath = getSafePath(req)
if (!stat.isFile()) {
throw Error('Path is not a file')
}

const stat = await fs.lstat(reqPath)
data = await fs.readFile(reqPath, 'utf8')
} else {
async function parseDir (dir) {
const toReturn = []
const files = await fs.readdir(dir)
for (const file of files) {
const entry = {
name: path.basename(file),
path: utils.joinPath(dir, file)
}
const stats = await fs.lstat(entry.path)
if (stats.isDirectory()) {
entry.children = await parseDir(entry.path)
} else {
entry.ext = file.split('.').pop()
}

entry.size = utils.humanSize(stats.size)
toReturn.push(entry)
}
return toReturn
}

if (!stat.isFile()) {
throw Error('Path is not a file')
data = await parseDir(storeDir)
}

const data = await fs.readFile(reqPath, 'utf8')

res.json({ success: true, data: data })
} catch (error) {
logger.error(error.message)
return res.json({ success: false, message: error.message })
}
})

app.put('/api/store/:path', storeLimiter, isAuthenticated, async function (
req,
res
) {
app.put('/api/store', storeLimiter, isAuthenticated, async function (req, res) {
try {
const reqPath = getSafePath(req)

Expand All @@ -907,7 +895,7 @@ app.put('/api/store/:path', storeLimiter, isAuthenticated, async function (
}
})

app.delete('/api/store/:path', storeLimiter, isAuthenticated, async function (
app.delete('/api/store', storeLimiter, isAuthenticated, async function (
req,
res
) {
Expand Down
14 changes: 9 additions & 5 deletions src/apis/ConfigApis.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,21 @@ export default {
return response.data
},
async getFile (path) {
const response = await request.get('/store/' + encodeURIComponent(path))
const response = await request.get('/store', { params: { path } })
return response.data
},
async writeFile (path, content) {
const response = await request.put('/store/' + encodeURIComponent(path), {
content
})
const response = await request.put(
'/store',
{
content
},
{ params: { path } }
)
return response.data
},
async deleteFile (path) {
const response = await request.delete('/store/' + encodeURIComponent(path))
const response = await request.delete('/store', { params: { path } })
return response.data
},
downloadZip (files) {
Expand Down

0 comments on commit b8dd575

Please sign in to comment.