Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
W
wiki-js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
1
Issues
1
List
Board
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
wiki-js
Commits
4f1c4198
Commit
4f1c4198
authored
May 24, 2019
by
Nick
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: handle asset upload overwrite
parent
5426b009
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
23 additions
and
4 deletions
+23
-4
assets.js
server/models/assets.js
+23
-4
No files found.
server/models/assets.js
View file @
4f1c4198
...
...
@@ -69,8 +69,14 @@ module.exports = class Asset extends Model {
const
fileInfo
=
path
.
parse
(
opts
.
originalname
)
const
fileHash
=
assetHelper
.
generateHash
(
opts
.
assetPath
)
// Create asset entry
const
asset
=
await
WIKI
.
models
.
assets
.
query
().
insert
({
// Check for existing asset
let
asset
=
await
WIKI
.
models
.
assets
.
query
().
where
({
hash
:
fileHash
,
folderId
:
opts
.
folderId
}).
first
()
// Build Object
let
assetRow
=
{
filename
:
opts
.
originalname
,
hash
:
fileHash
,
ext
:
fileInfo
.
ext
,
...
...
@@ -79,21 +85,34 @@ module.exports = class Asset extends Model {
fileSize
:
opts
.
size
,
folderId
:
opts
.
folderId
,
authorId
:
opts
.
userId
}
)
}
// Save asset data
try
{
const
fileBuffer
=
await
fs
.
readFile
(
opts
.
path
)
if
(
asset
)
{
// Patch existing asset
await
WIKI
.
models
.
assets
.
query
().
patch
(
assetRow
).
findById
(
asset
.
id
)
await
WIKI
.
models
.
knex
(
'assetData'
).
where
({
id
:
asset
.
id
}).
update
({
data
:
fileBuffer
})
}
else
{
// Create asset entry
asset
=
await
WIKI
.
models
.
assets
.
query
().
insert
(
assetRow
)
await
WIKI
.
models
.
knex
(
'assetData'
).
insert
({
id
:
asset
.
id
,
data
:
fileBuffer
})
}
}
catch
(
err
)
{
WIKI
.
logger
.
warn
(
err
)
}
// Move temp upload to cache
await
fs
.
move
(
opts
.
path
,
path
.
join
(
process
.
cwd
(),
`data/cache/
${
fileHash
}
.dat`
))
await
fs
.
move
(
opts
.
path
,
path
.
join
(
process
.
cwd
(),
`data/cache/
${
fileHash
}
.dat`
)
,
{
overwrite
:
true
}
)
}
static
async
getAsset
(
assetPath
,
res
)
{
...
...
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