Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent 404 errors in store when using nginx #1060

Merged
merged 1 commit into from
Apr 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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