Skip to content

Commit

Permalink
上传
Browse files Browse the repository at this point in the history
  • Loading branch information
Dooy committed Dec 6, 2023
1 parent 5f5fab4 commit 90c94b7
Show file tree
Hide file tree
Showing 12 changed files with 248 additions and 24 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ coverage

# Environment variables files
/service/.env
/service/uploads
7 changes: 6 additions & 1 deletion service/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,10 @@ HTTPS_PROXY=
MJ_SERVER=

# MJ_API_SECRET
MJ_API_SECRET=
MJ_API_SECRET=

#API_UPLOADER 是否可以上传 1 可以其他都不可以
API_UPLOADER=

#HIDE_SERVER 隐藏服务端 1
HIDE_SERVER=
1 change: 1 addition & 0 deletions service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"http-proxy-middleware": "^2.0.6",
"https-proxy-agent": "^5.0.1",
"isomorphic-fetch": "^3.0.0",
"multer": "1.4.5-lts.1",
"node-fetch": "^3.3.0",
"socks-proxy-agent": "^7.0.0"
},
Expand Down
95 changes: 92 additions & 3 deletions service/pnpm-lock.yaml

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

56 changes: 52 additions & 4 deletions service/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import type { ChatMessage } from './chatgpt'
import { chatConfig, chatReplyProcess, currentModel } from './chatgpt'
import { auth } from './middleware/auth'
import { limiter } from './middleware/limiter'
import { isNotEmptyString } from './utils/is'
import { isNotEmptyString,formattedDate } from './utils/is'
import multer from "multer"
import path from "path"
import fs from "fs"
// const { createProxyMiddleware } = require('http-proxy-middleware');
//import {createProxyMiddleware} from "http-proxy-middleware"
import proxy from "express-http-proxy"
Expand All @@ -18,6 +21,8 @@ app.use(express.static('public'))
//app.use(express.json())
app.use(bodyParser.json({ limit: '10mb' })); //大文件传输



app.all('*', (_, res, next) => {
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Headers', 'authorization, Content-Type')
Expand Down Expand Up @@ -65,7 +70,9 @@ router.post('/session', async (req, res) => {
try {
const AUTH_SECRET_KEY = process.env.AUTH_SECRET_KEY
const hasAuth = isNotEmptyString(AUTH_SECRET_KEY)
res.send({ status: 'Success', message: '', data: { auth: hasAuth, model: currentModel() } })
const isUpload= isNotEmptyString( process.env.API_UPLOADER )
const isHideServer= isNotEmptyString( process.env.HIDE_SERVER )
res.send({ status: 'Success', message: '', data: {isHideServer,isUpload, auth: hasAuth, model: currentModel() } })
}
catch (error) {
res.send({ status: 'Fail', message: error.message, data: null })
Expand Down Expand Up @@ -106,6 +113,48 @@ app.use('/mjapi', proxy(process.env.MJ_SERVER?process.env.MJ_SERVER:'https://api

}));



// 设置存储引擎和文件保存路径
const storage = multer.diskStorage({
destination: function (req, file, cb) {
let uploadFolderPath=`./uploads/${formattedDate()}/`;//`

console.log('dir', __dirname ) ;

if(!fs.existsSync('./uploads/')) {
fs.mkdirSync('./uploads/');
}
if(!fs.existsSync(uploadFolderPath)) {
fs.mkdirSync(uploadFolderPath);
}
cb(null, `uploads/${formattedDate()}/`);
},
filename: function (req, file, cb) {
let filename= Date.now() + path.extname(file.originalname);
console.log( 'file', filename );
cb(null, filename);
}
});
const upload = multer({ storage: storage });
// 处理文件上传的路由
const isUpload= isNotEmptyString( process.env.API_UPLOADER )
if(isUpload){
app.use('/openapi/v1/upload', upload.single('file'), (req, res) => {
//res.send('文件上传成功!');
res.setHeader('Content-type', 'application/json' );
if(req.file.filename) res.json({ url:`/uploads/${formattedDate()}/${ req.file.filename }`,created:Date.now() })
else res.json({ error:`uploader fail`,created:Date.now() })
});
}else {
app.use('/openapi/v1/upload', (req, res) => {
//res.send('文件上传成功!');
res.json({ error:`server is no open uploader `,created:Date.now() })
});
}
app.use('/uploads', express.static('uploads'));


//代理openai 接口
app.use('/openapi', proxy(API_BASE_URL, {
https: false, limit: '10mb',
Expand All @@ -117,8 +166,7 @@ app.use('/openapi', proxy(API_BASE_URL, {
proxyReqOpts.headers['Content-Type'] = 'application/json';
return proxyReqOpts;
},
//limit: '10mb'

//limit: '10mb'
}));


Expand Down
9 changes: 9 additions & 0 deletions service/src/utils/is.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,12 @@ export function isBoolean<T extends boolean>(value: T | unknown): value is boole
export function isFunction<T extends (...args: any[]) => any | void | never>(value: T | unknown): value is T {
return Object.prototype.toString.call(value) === '[object Function]'
}

export const formattedDate=()=>{
const currentDate = new Date();
const year = currentDate.getFullYear();
const month = (currentDate.getMonth() + 1).toString().padStart(2, '0');
const day = currentDate.getDate().toString().padStart(2, '0');
return `${year}${month}${day}`;

}
Loading

0 comments on commit 90c94b7

Please sign in to comment.