Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
赵月辉
/
fastgpt-migrated
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Unverified
Commit
7c16d08e
authored
Sep 03, 2023
by
archer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: crud file
parent
5157e62f
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
104 additions
and
7 deletions
+104
-7
client/src/pages/api/plugins/file/delete.ts
+30
-0
client/src/pages/api/plugins/file/read.ts
+4
-7
client/src/pages/api/plugins/file/readUrl.ts
+70
-0
No files found.
client/src/pages/api/plugins/file/delete.ts
0 → 100644
View file @
7c16d08e
import
type
{
NextApiRequest
,
NextApiResponse
}
from
'next'
;
import
{
jsonRes
}
from
'@/service/response'
;
import
{
connectToDatabase
}
from
'@/service/mongo'
;
import
{
authUser
}
from
'@/service/utils/auth'
;
import
{
GridFSStorage
}
from
'@/service/lib/gridfs'
;
export
default
async
function
handler
(
req
:
NextApiRequest
,
res
:
NextApiResponse
<
any
>
)
{
try
{
await
connectToDatabase
();
const
{
fileId
}
=
req
.
query
as
{
fileId
:
string
};
if
(
!
fileId
)
{
throw
new
Error
(
'fileId is empty'
);
}
const
{
userId
}
=
await
authUser
({
req
});
const
gridFs
=
new
GridFSStorage
(
'dataset'
,
userId
);
await
gridFs
.
delete
(
fileId
);
jsonRes
(
res
);
}
catch
(
error
)
{
jsonRes
(
res
,
{
code
:
500
,
error
});
}
}
client/src/pages/api/plugins/file/read.ts
View file @
7c16d08e
import
type
{
NextApiRequest
,
NextApiResponse
}
from
'next'
;
import
type
{
NextApiRequest
,
NextApiResponse
}
from
'next'
;
import
{
jsonRes
}
from
'@/service/response'
;
import
{
jsonRes
}
from
'@/service/response'
;
import
{
connectToDatabase
}
from
'@/service/mongo'
;
import
{
connectToDatabase
}
from
'@/service/mongo'
;
import
{
authUser
}
from
'@/service/utils/auth'
;
import
{
GridFSStorage
}
from
'@/service/lib/gridfs'
;
import
{
GridFSStorage
}
from
'@/service/lib/gridfs'
;
import
{
authFileToken
}
from
'./readUrl'
;
export
default
async
function
handler
(
req
:
NextApiRequest
,
res
:
NextApiResponse
<
any
>
)
{
export
default
async
function
handler
(
req
:
NextApiRequest
,
res
:
NextApiResponse
<
any
>
)
{
try
{
try
{
await
connectToDatabase
();
await
connectToDatabase
();
const
{
fileId
}
=
req
.
query
as
{
fileId
:
string
};
const
{
token
}
=
req
.
query
as
{
token
:
string
};
if
(
!
fileId
)
{
const
{
fileId
,
userId
}
=
await
authFileToken
(
token
);
throw
new
Error
(
'fileId is empty'
);
}
const
{
userId
}
=
await
authUser
({
req
});
const
gridFs
=
new
GridFSStorage
(
'dataset'
,
userId
);
const
gridFs
=
new
GridFSStorage
(
'dataset'
,
userId
);
...
@@ -25,6 +21,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
...
@@ -25,6 +21,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
res
.
setHeader
(
'encoding'
,
file
.
encoding
);
res
.
setHeader
(
'encoding'
,
file
.
encoding
);
res
.
setHeader
(
'Content-Type'
,
file
.
contentType
);
res
.
setHeader
(
'Content-Type'
,
file
.
contentType
);
res
.
setHeader
(
'Cache-Control'
,
'public, max-age=3600'
);
res
.
end
(
buffer
);
res
.
end
(
buffer
);
}
catch
(
error
)
{
}
catch
(
error
)
{
...
...
client/src/pages/api/plugins/file/readUrl.ts
0 → 100644
View file @
7c16d08e
import
type
{
NextApiRequest
,
NextApiResponse
}
from
'next'
;
import
{
jsonRes
}
from
'@/service/response'
;
import
{
connectToDatabase
}
from
'@/service/mongo'
;
import
{
authUser
}
from
'@/service/utils/auth'
;
import
jwt
from
'jsonwebtoken'
;
import
{
ERROR_ENUM
}
from
'@/service/errorCode'
;
export
default
async
function
handler
(
req
:
NextApiRequest
,
res
:
NextApiResponse
<
any
>
)
{
try
{
await
connectToDatabase
();
const
{
fileId
}
=
req
.
query
as
{
fileId
:
string
};
if
(
!
fileId
)
{
throw
new
Error
(
'fileId is empty'
);
}
const
{
userId
}
=
await
authUser
({
req
});
const
token
=
await
createFileToken
({
userId
,
fileId
});
jsonRes
(
res
,
{
data
:
`/api/plugins/file/read?token=
${
token
}
`
});
}
catch
(
error
)
{
jsonRes
(
res
,
{
code
:
500
,
error
});
}
}
export
const
createFileToken
=
(
data
:
{
userId
:
string
;
fileId
:
string
})
=>
{
if
(
!
process
.
env
.
FILE_TOKEN_KEY
)
{
return
Promise
.
reject
(
'System unset FILE_TOKEN_KEY'
);
}
const
expiredTime
=
Math
.
floor
(
Date
.
now
()
/
1000
)
+
60
*
30
;
const
key
=
process
.
env
.
FILE_TOKEN_KEY
as
string
;
const
token
=
jwt
.
sign
(
{
...
data
,
exp
:
expiredTime
},
key
);
return
Promise
.
resolve
(
token
);
};
export
const
authFileToken
=
(
token
?:
string
)
=>
new
Promise
<
{
userId
:
string
;
fileId
:
string
}
>
((
resolve
,
reject
)
=>
{
if
(
!
token
)
{
return
reject
(
ERROR_ENUM
.
unAuthFile
);
}
const
key
=
process
.
env
.
FILE_TOKEN_KEY
as
string
;
jwt
.
verify
(
token
,
key
,
function
(
err
,
decoded
:
any
)
{
if
(
err
||
!
decoded
?.
userId
||
!
decoded
?.
fileId
)
{
reject
(
ERROR_ENUM
.
unAuthFile
);
return
;
}
resolve
({
userId
:
decoded
.
userId
,
fileId
:
decoded
.
fileId
});
});
});
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment