-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a83e45e
commit 09cece6
Showing
1 changed file
with
6 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,21 @@ | ||
// This module is intended to get picture url | ||
const Joi = require('joi') | ||
const crypto = require('crypto') | ||
const { getPictureURL } = require('./_sdk_wrapper') | ||
// validation schema | ||
const { ValidateParams } = require('../../utils/response') | ||
const schema = Joi.object({ | ||
id: Joi.string().required(), | ||
height: Joi.string().default(300), | ||
height: Joi.number().default(300), | ||
}) | ||
|
||
const md5 = (data) => { | ||
const buf = Buffer.from(data) | ||
const str = buf.toString('binary') | ||
return crypto.createHash('md5').update(str).digest('base64') | ||
} | ||
|
||
const neteasePickey = (id) => { | ||
id = String(id) | ||
const magic = '3go8&$8*3*3h0k(2)2'.split('') | ||
const songId = id | ||
.split('') | ||
.map((item, index) => | ||
String.fromCharCode( | ||
item.charCodeAt(0) ^ magic[index % magic.length].charCodeAt(0), | ||
), | ||
) | ||
return md5(songId.join('')).replace(/\//g, '_').replace(/\+/g, '-') | ||
} | ||
|
||
const picture = (id, size = 300) => { | ||
return Promise.resolve({ | ||
url: `https://p3.music.126.net/${neteasePickey( | ||
id, | ||
)}/${id}.jpg?param=${size}y${size}`, | ||
}) | ||
} | ||
|
||
module.exports = async (ctx) => { | ||
const params = Object.assign({}, ctx.params, ctx.query, ctx.request.body) | ||
if (!(await ValidateParams(params, schema, ctx))) { | ||
// validateParams | ||
return | ||
} | ||
const pictureURL = await picture(params.id, params.height) | ||
ctx.body = pictureURL | ||
const pictureURL = getPictureURL(params.id, params.height) | ||
ctx.body = { | ||
url: pictureURL, | ||
} | ||
} |