提交 aac0208e 作者: 方治民

feat: 天地图缓存增加默认 key 防止泄露问题、新增地形数据缓存

上级 74efc8f1
{
"parserOptions": {
"ecmaVersion": 2021,
"sourceType": "module"
},
"env": {
"node": true,
"es6": true
......
......@@ -5,6 +5,7 @@ const sprites = require('./sprites')
const fonts = require('./fonts')
const assets = require('./assets')
const tdt = require('./tdt')
const terrain = require('./terrain')
const router = express.Router()
......@@ -14,5 +15,6 @@ router.use(sprites)
router.use(fonts)
router.use(assets)
router.use(tdt)
router.use(terrain)
module.exports = router
......@@ -44,7 +44,7 @@ function downloadFile(url, filepath) {
}
module.exports.cache = async (req, res, next) => {
const { id, x, y, z, tk, v = 'DEFAULE' } = req.params
const { id, x, y, z, tk = '5e8ccf6c93d1ce905f4e5ba0ce30159a', v = 'DEFAULE' } = req.params
const host = 't0'
const uri = `https://${host}.tianditu.gov.cn/${id}_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=${id}&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX=${z}&TILEROW=${y}&TILECOL=${x}&tk=${tk}`
const src = path.resolve(`./data/cache/tdt/${v}/${id}/${z}/${x}/${y}.png`)
......
const fs = require('fs')
const path = require('path')
const https = require('https')
function downloadFile(url, filepath) {
if (!fs.existsSync(filepath)) {
fs.mkdirSync(path.dirname(filepath), { recursive: true })
}
return new Promise(function (resolve, reject) {
//忽略证书认证
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0
try {
https.get(
url,
{
headers: {
'User-Agent':
'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1 Edg/102.0.5005.124'
}
},
function (res) {
if (res.statusCode === 200) {
//使用数据流写入
const file = fs.createWriteStream(filepath)
file.on('finish', function () {
file.close()
resolve()
})
file.on('error', reject)
res.pipe(file)
} else {
res.resume()
console.log(`Request Failed With a Status Code: ${res.statusCode}`)
reject()
}
}
)
} catch (err) {
console.log(err)
reject()
}
})
}
module.exports.cache = async (req, res, next) => {
const { x, y, z, v = 'DEFAULE' } = req.params
const object = `${x}/${y}/${z}`
const uri = `https://data.mars3d.cn/terrain/${object}`
const src = path.resolve(`./data/cache/terrain/${v}/${object}`)
try {
if (!fs.existsSync(src)) {
await downloadFile(uri, src)
}
} catch (_) {
try {
if (fs.existsSync(src)) {
fs.rmSync(src)
}
} catch (__) {}
}
res.sendFile(src, function (err) {
if (err) return next(err)
})
}
const router = require('express').Router()
const terrain = require('./controller')
router.get('/terrain/cache/:x/:y/:z', terrain.cache)
module.exports = router
{
"version": 8,
"zoom": 4,
"center": [0, 0],
"sources": {
"image": {
"type": "raster",
"url": "/api/tilesets/image/tilejson",
"tileSize": 256
}
},
"layers": [
{
"id": "bg",
"type": "background",
"paint": { "background-color": "black" }
},
{
"id": "background",
"type": "background",
"layout": { "visibility": "none" },
"paint": { "background-color": "hsla(0, 0%, 0%, 0)" }
},
{
"id": "image",
"type": "raster",
"source": "image"
}
]
}
\ No newline at end of file
......@@ -9,7 +9,7 @@
"docs": "docsify serve ./docs --open",
"test": "tape test/**/*.js",
"lint": "eslint app/**/*.js",
"format": "prettier-eslint --write app/**/*.js",
"format": "prettier-eslint -l info --write app/**/*.js",
"dist": "pkg . --out-path=dist/ --targets=win,linux,macos --compress=GZip",
"dist:linux": "pkg . --out-path=dist/ --targets=linux --compress=GZip",
"dist:win": "pkg . --out-path=dist/ --targets=win --compress=GZip"
......@@ -23,7 +23,7 @@
]
},
"lint-staged": {
"{app,test}/**/*.js": "prettier-eslint --write"
"{test}/**/*.js": "prettier-eslint --write"
},
"dependencies": {
"@mapbox/glyph-pbf-composite": "^0.0.3",
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论