Skip to content

Commit

Permalink
refactor: refactor packages structure
Browse files Browse the repository at this point in the history
  • Loading branch information
reruin committed Aug 31, 2021
1 parent e4b46cc commit 45b780d
Show file tree
Hide file tree
Showing 22 changed files with 685 additions and 59 deletions.
10 changes: 10 additions & 0 deletions .github/ISSUE_TEMPLATE/bug_report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ body:
label: 问题描述 / Describe the bug
validations:
required: true
- type: dropdown
id: version
attributes:
label: Sharelist 版本 / Sharelist Version
description: Sharelist Version
options:
- v0.1
- v0.2
validations:
required: true
- type: textarea
id: reproduction
attributes:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ node_modules
build/
dist/
/packages/sharelist/theme/
/packages/sharelist/cache/
.vscode/
*.lock
*.log
Expand Down
127 changes: 119 additions & 8 deletions packages/sharelist-core/lib/driver.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
const utils = require('./utils')
const request = require('./request')

const clone = (obj) => {
// console.log(obj)
let type = typeof obj
Expand All @@ -21,6 +24,16 @@ const clone = (obj) => {
}

module.exports = (app) => {

/**
* 列出指定id
* @param {object} options
* @param {string} options.id
* @param {array} options.paths
* @param {object} options.query
*
* @returns { files | error }
*/
const list = async ({ paths = [], id, query } = {}) => {
const filter = app.hookLifetime('onListed')

Expand Down Expand Up @@ -77,20 +90,38 @@ module.exports = (app) => {
return parent?.files.find(i => i.name == filename)?.id
}

const getStream = async (id, options) => {
/**
* 返回指定id的内容
* @param {string} id file id
* @param {object} options
* @param {object | undefined} options.reqHeaders
* @param {number} options.start offset start
* @param {number} options.end offset end
*
* @returns { stream , acceptRanges, error }
*/
const createReadStream = async (id, options = {}) => {
let data = await get({ id })

if (data.error) return data

if (data.download_url) {
let reqHeaders = {}
let reqHeaders = options.reqHeaders || {}

if (data.extra.proxy?.headers) {
Object.assign(reqHeaders, data.extra.proxy.headers)
}

let { data, headers } = await app.request(data.download_url, { headers: reqHeaders, responseType: 'stream' })
if (data) {
return { stream: data, acceptRanges: headers?.['accept-ranges'] == 'bytes' }
if (options.start) {
reqHeaders['range'] = `bytes=${options.start}-${options.end || ''}`
}

console.log('reqHeaders', reqHeaders)

let { data: stream, headers, status, error } = await request(data.download_url, { headers: reqHeaders, responseType: 'stream' })

if (!error) {
return { stream, headers, status, acceptRanges: headers?.['accept-ranges'] == 'bytes' }
}
} else {
const [protocol] = parseProtocol(data.id)
Expand All @@ -101,7 +132,7 @@ module.exports = (app) => {
}
}

return { error: { message: "CAN'T GET STREAM" } }
return { error: { code: 501, message: "Not implemented" } }
}

//获取文本内容
Expand All @@ -113,6 +144,74 @@ module.exports = (app) => {
return await app.utils.transfromStreamToString(stream)
}

const createWriteStream = async (id, options) => {
const [protocol, fid] = parseProtocol(id)

let driver = app.getDriver(protocol)

if (driver?.createWriteStream) {
let writeStream = await driver.createWriteStream(id, options)

if (writeStream.error) return writeStream

return writeStream
}

return { error: { code: 501, message: "Not implemented" } }
}

const mkdir = async (id, options) => {
const [protocol, fid] = parseProtocol(id)
let driver = app.getDriver(protocol)

if (driver?.mkdir) {
return await driver.mkdir(id, options)
}

return { error: { code: 501, message: "Not implemented" } }
}

const rm = async (id, options = {}) => {
const [protocol, fid] = parseProtocol(id)
let driver = app.getDriver(protocol)

if (!driver?.rm) {
return { error: { code: 501, message: "Not implemented" } }
}

return await driver.rm(id, options)
}

const rename = async (id, name) => {
const [protocol, fid] = parseProtocol(id)

let driver = app.getDriver(protocol)

if (!driver?.rename) {
return { error: { code: 501, message: "Not implemented" } }
}

return await driver.rename(id, name)

}

const mv = async (id, target) => {
const [protocol, fid] = parseProtocol(id)

const [targetProtocol] = parseProtocol(target)

//only support same protocol
if (targetProtocol === protocol) {
let driver = app.getDriver(protocol)

if (driver?.mv) {
return await driver.mv(id, target)
}
}

return { error: { code: 501, message: "Not implemented" } }
}

const root = () => {
let disk = app.config.drives.map((i) => ({
id: i.path.split('?')[0],
Expand All @@ -138,7 +237,7 @@ module.exports = (app) => {

if (!driver) return { error: { code: 500, message: 'miss driver' } }

let data = await driver.path(fid, { ...query })
let data = await driver.list(fid, { ...query })

if (filter) {
await filter(data, query, filter)
Expand Down Expand Up @@ -202,5 +301,17 @@ module.exports = (app) => {
return hit
}

return { list, get, getStream, getContent }
return {
list,
get,
mkdir,
rm,
rename,
mv,
createReadStream,
createWriteStream,

getStream: createReadStream,
getContent
}
}
2 changes: 1 addition & 1 deletion packages/sharelist-core/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ module.exports = async (options) => {
cache,
request,
utils,
ocr: ocr,
ocr,

decode,
encode,
Expand Down
18 changes: 16 additions & 2 deletions packages/sharelist-core/lib/request.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const fetch = require('node-fetch')

const https = require('https')

const btoa = (v) => Buffer.from(v).toString('base64')

const querystring = require('querystring')
Expand Down Expand Up @@ -46,6 +48,16 @@ const request = async (url, options = {}) => {

let args = { method, size: 0, agent, compress, timeout, headers }

// if (!args.agent) {
// args.agent = function (_parsedURL) {
// if (_parsedURL.protocol == 'https:') {
// return new https.Agent({
// rejectUnauthorized: false,
// })
// }
// }
// }

if (auth) {
args.headers['authorization'] = `Basic ${btoa(auth)}`
}
Expand All @@ -60,7 +72,7 @@ const request = async (url, options = {}) => {
if (!args.headers['content-type'] && method != 'GET') {
if (contentType === 'json') {
args.headers['content-type'] = 'application/json'
} else {
} else if (contentType != 'stream') {
args.headers['content-type'] = 'application/x-www-form-urlencoded'
}
}
Expand All @@ -71,12 +83,14 @@ const request = async (url, options = {}) => {
} else if (['POST', 'PUT', 'DELETE'].includes(method)) {
if (args.headers['content-type'].includes('application/json')) {
args.body = JSON.stringify(data)
} else if (contentType == 'stream') {
args.body = data
retry = 0
} else {
args.body = querystring.stringify(data)
}
}
}

while (true) {
try {
// console.log(url, args)
Expand Down
57 changes: 57 additions & 0 deletions packages/sharelist-core/package-lock.json

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

Loading

0 comments on commit 45b780d

Please sign in to comment.