Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
D
duiba-tiny-image-node
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
劳工
duiba-tiny-image-node
Commits
0b1d9fd0
Commit
0b1d9fd0
authored
Nov 07, 2021
by
rockyl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修改6
parent
ef6811c1
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
127 additions
and
62 deletions
+127
-62
api.js
dist/api.js
+60
-30
api.js.map
dist/api.js.map
+1
-1
test.http
http-test/test.http
+8
-3
api.ts
src/api.ts
+58
-28
No files found.
dist/api.js
View file @
0b1d9fd0
...
...
@@ -19,48 +19,78 @@ function applyApi(server) {
server
.
post
(
'/tinify'
,
tinify
);
}
exports
.
default
=
applyApi
;
//一个文件可以返回blob,多个文件必须传cdnPath
async
function
tinify
(
req
,
res
,
next
)
{
let
file
=
req
.
files
.
file
;
const
{
type
=
'builtin'
,
cdnPath
}
=
req
.
query
;
if
(
file
)
{
if
(
acceptTypes
.
includes
(
file
.
type
))
{
try
{
const
buffer
=
await
(
0
,
compress_1
.
compressImage
)(
file
.
path
,
type
);
if
(
cdnPath
)
{
const
name
=
await
(
0
,
ali_oss_duiba_1
.
uploadFile
)(
buffer
,
cdnPath
);
res
.
send
({
code
:
0
,
data
:
name
});
}
else
{
res
.
sendRaw
(
buffer
,
{
'Content-Type'
:
file
.
type
,
});
}
}
catch
(
e
)
{
logger_1
.
default
.
error
(
'/tinify'
,
'compress failed'
,
e
.
message
);
res
.
send
({
code
:
3
,
msg
:
'compress failed'
});
}
const
fileMap
=
req
.
files
;
const
files
=
Object
.
entries
(
fileMap
).
filter
(([
key
,
file
])
=>
{
const
accept
=
acceptTypes
.
includes
(
file
.
type
);
if
(
accept
)
{
file
.
key
=
key
;
}
return
accept
;
}).
map
(([
key
,
file
])
=>
file
);
const
fileCount
=
files
.
length
;
if
(
fileCount
===
0
)
{
res
.
send
({
code
:
1
,
msg
:
'file not exists'
});
}
else
if
(
fileCount
===
1
)
{
const
[
file
]
=
files
;
const
result
=
await
tinifyOnce
(
file
.
path
,
type
,
cdnPath
);
if
(
typeof
result
===
'string'
)
{
res
.
send
({
code
:
0
,
data
:
result
});
}
else
if
(
Buffer
.
isBuffer
(
result
))
{
res
.
sendRaw
(
result
,
{
'Content-Type'
:
file
.
type
,
});
}
else
{
logger_1
.
default
.
error
(
'/tinify'
,
'
type error'
);
logger_1
.
default
.
error
(
'/tinify'
,
'
compress failed'
,
result
.
message
);
res
.
send
({
code
:
2
,
msg
:
'
type error
'
code
:
3
,
msg
:
'
compress failed
'
});
}
}
else
if
(
cdnPath
)
{
const
urls
=
await
Promise
.
all
(
files
.
map
(
file
=>
tinifyOnce
(
file
.
path
,
type
,
cdnPath
)
.
then
(
result
=>
{
if
(
typeof
result
===
'string'
)
{
return
{
key
:
file
.
key
,
url
:
result
,
};
}
})));
console
.
log
(
urls
);
}
else
{
res
.
send
({
code
:
1
,
msg
:
'
file not exist
s'
code
:
4
,
msg
:
'
specify cdnPath when multi file
s'
});
}
return
next
();
}
async
function
tinifyOnce
(
filePath
,
type
,
cdnPath
)
{
try
{
const
buffer
=
await
(
0
,
compress_1
.
compressImage
)(
filePath
,
type
);
if
(
cdnPath
)
{
return
(
0
,
ali_oss_duiba_1
.
uploadFile
)(
buffer
,
cdnPath
);
}
else
{
return
buffer
;
}
}
catch
(
e
)
{
return
e
;
}
}
//# sourceMappingURL=api.js.map
\ No newline at end of file
dist/api.js.map
View file @
0b1d9fd0
{"version":3,"file":"api.js","sourceR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,CAAA;SACb;KACD;IAAC,OAAO,CAAC,EAAE;QACX,OAAO,CAAC,CAAA;KACR;AACF,CAAC"}
\ No newline at end of file
http-test/test.http
View file @
0b1d9fd0
### 查询项目
POST http://localhost:8090/tinify?
type=tinypng&
cdnPath=spark/assets
POST http://localhost:8090/tinify?cdnPath=spark/assets
Accept: */*
Cache-Control: no-cache
Content-Type: multipart/form-data; boundary=WebAppBoundary
--WebAppBoundary--
Content-Disposition: form-data; name="file
"; filename="Github.pn
g"
Content-Type: image/
pn
g
Content-Disposition: form-data; name="file
1"; filename="1643017.jp
g"
Content-Type: image/
jpe
g
< /Users/rockyl/Pictures/1643017.jpg
--WebAppBoundary--
Content-Disposition: form-data; name="file2"; filename="烽火台-logo.png"
Content-Type: image/png
< /Users/rockyl/Pictures/烽火台-logo.png
--WebAppBoundary--
src/api.ts
View file @
0b1d9fd0
...
...
@@ -22,44 +22,74 @@ export default function applyApi(server) {
server
.
post
(
'/tinify'
,
tinify
)
}
//一个文件可以返回blob,多个文件必须传cdnPath
async
function
tinify
(
req
,
res
,
next
)
{
let
file
=
req
.
files
.
file
const
{
type
=
'builtin'
,
cdnPath
}
=
req
.
query
if
(
file
)
{
if
(
acceptTypes
.
includes
(
file
.
type
))
{
try
{
const
buffer
=
await
compressImage
(
file
.
path
,
type
)
if
(
cdnPath
)
{
const
name
=
await
uploadFile
(
buffer
,
cdnPath
)
res
.
send
({
code
:
0
,
data
:
name
})
}
else
{
res
.
sendRaw
(
buffer
,
{
'Content-Type'
:
file
.
type
,
})
}
}
catch
(
e
)
{
logger
.
error
(
'/tinify'
,
'compress failed'
,
e
.
message
)
res
.
send
({
code
:
3
,
msg
:
'compress failed'
})
}
const
fileMap
:
{
[
key
:
string
]:
any
}
=
req
.
files
const
files
=
Object
.
entries
(
fileMap
).
filter
(([
key
,
file
])
=>
{
const
accept
=
acceptTypes
.
includes
(
file
.
type
)
if
(
accept
)
{
file
.
key
=
key
}
return
accept
}).
map
(([
key
,
file
])
=>
file
)
const
fileCount
=
files
.
length
if
(
fileCount
===
0
)
{
res
.
send
({
code
:
1
,
msg
:
'file not exists'
})
}
else
if
(
fileCount
===
1
)
{
const
[
file
]
=
files
const
result
=
await
tinifyOnce
(
file
.
path
,
type
,
cdnPath
)
if
(
typeof
result
===
'string'
)
{
res
.
send
({
code
:
0
,
data
:
result
})
}
else
if
(
Buffer
.
isBuffer
(
result
))
{
res
.
sendRaw
(
result
,
{
'Content-Type'
:
file
.
type
,
})
}
else
{
logger
.
error
(
'/tinify'
,
'
type error'
)
logger
.
error
(
'/tinify'
,
'
compress failed'
,
result
.
message
)
res
.
send
({
code
:
2
,
msg
:
'
type error
'
code
:
3
,
msg
:
'
compress failed
'
})
}
}
else
if
(
cdnPath
)
{
const
urls
=
await
Promise
.
all
(
files
.
map
(
file
=>
tinifyOnce
(
file
.
path
,
type
,
cdnPath
)
.
then
(
result
=>
{
if
(
typeof
result
===
'string'
)
{
return
{
key
:
file
.
key
,
url
:
result
,
}
}
})
))
console
.
log
(
urls
)
}
else
{
res
.
send
({
code
:
1
,
msg
:
'
file not exist
s'
code
:
4
,
msg
:
'
specify cdnPath when multi file
s'
})
}
return
next
()
}
async
function
tinifyOnce
(
filePath
,
type
,
cdnPath
)
{
try
{
const
buffer
=
await
compressImage
(
filePath
,
type
)
if
(
cdnPath
)
{
return
uploadFile
(
buffer
,
cdnPath
)
}
else
{
return
buffer
}
}
catch
(
e
)
{
return
e
}
}
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