Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
C
console
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
MyCard
console
Commits
7c5aeef1
Commit
7c5aeef1
authored
Apr 25, 2017
by
nano
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix docker
parent
1cbd73fa
Changes
3
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
326 additions
and
38 deletions
+326
-38
.idea/workspace.xml
.idea/workspace.xml
+114
-36
entrypoint.sh
entrypoint.sh
+1
-2
src/routes/upload.ts
src/routes/upload.ts
+211
-0
No files found.
.idea/workspace.xml
View file @
7c5aeef1
This diff is collapsed.
Click to expand it.
entrypoint.sh
View file @
7c5aeef1
#!/usr/bin/sh
aria2c
--conf-path
=
/usr/src/app/aria2.conf
-D
aria2c
--conf-path
=
/usr/src/app/aria2.conf
-D
;
npm start
src/routes/upload.ts
0 → 100644
View file @
7c5aeef1
import
{
Context
}
from
'
koa
'
;
import
{
OSS
}
from
'
aliyun-sdk
'
;
import
*
as
busboy
from
'
async-busboy
'
;
import
*
as
mime
from
'
mime
'
;
import
*
as
uuid
from
'
uuid
'
;
import
*
as
Client
from
'
aliyun-oss-upload-stream
'
;
import
*
as
fs
from
'
fs-extra-promise
'
;
import
*
as
path
from
'
path
'
;
import
*
as
Aria2
from
'
aria2
'
;
import
{
bundle
}
from
'
../../package/main
'
;
import
{
mongodb
}
from
'
../models/Iridium
'
;
import
{
toObjectID
}
from
'
iridium
'
;
import
config
from
'
../../config
'
;
// const Aria2Options = {
// host: 'localhost',
// port: 6800,
// secure: false,
// secret: '',
// path: path.join(__dirname, '../../test/upload')
// }
const
checkPackage
=
async
(
file
)
=>
{
if
([
'
application/zip
'
,
'
application/gz
'
,
'
application/rar
'
,
'
application/7z
'
,
'
application/x-gzip
'
].
indexOf
(
file
.
mime
)
===
-
1
)
{
console
.
log
(
file
.
mime
)
throw
new
Error
(
`Unsupported file type:
${
file
.
mime
}
`
);
}
};
const
checkImage
=
async
(
file
)
=>
{
const
ext
=
mime
.
extension
(
file
.
mime
);
if
([
'
png
'
,
'
jpg
'
,
'
jpeg
'
,
'
gif
'
,
'
webp
'
].
indexOf
(
ext
)
===
-
1
)
{
throw
new
Error
(
'
Unsupported file type
'
);
}
};
import
Router
=
require
(
'
koa-router
'
);
const
ossStream
=
Client
(
new
OSS
({
accessKeyId
:
process
.
env
[
'
OSS_ACCESS_ID
'
],
secretAccessKey
:
process
.
env
[
'
OSS_ACCESS_KEY
'
],
endpoint
:
process
.
env
[
'
OSS_ENDPOINT
'
],
apiVersion
:
'
2013-10-15
'
}));
const
router
=
new
Router
();
const
UploadImage
=
async
(
ctx
:
Context
)
=>
{
try
{
const
{
files
}
=
await
busboy
(
ctx
.
req
);
ctx
.
body
=
await
Promise
.
all
(
files
.
map
(
async
file
=>
{
await
checkImage
(
file
);
const
filename
=
`test/
${
uuid
.
v1
()}
`
;
const
upload
=
ossStream
.
upload
({
Bucket
:
process
.
env
[
'
OSS_BUCKET
'
],
Key
:
filename
,
ContentType
:
file
.
mimeType
});
file
.
pipe
(
upload
);
return
await
new
Promise
((
resolve
,
reject
)
=>
{
upload
.
on
(
'
error
'
,
reject
);
upload
.
on
(
'
uploaded
'
,
resolve
);
});
}));
}
catch
(
err
)
{
ctx
.
throw
(
403
,
err
);
}
};
export
const
UploadPackage
=
async
(
ctx
:
Context
)
=>
{
try
{
const
{
files
}
=
await
busboy
(
ctx
.
req
);
ctx
.
body
=
await
Promise
.
all
(
files
.
map
(
async
file
=>
{
await
checkPackage
(
file
);
const
filename
=
uuid
.
v1
();
const
archive_path
=
path
.
join
(
__dirname
,
'
../../test/upload
'
);
await
fs
.
ensureDirAsync
(
archive_path
);
const
archive
=
fs
.
createWriteStream
(
path
.
join
(
archive_path
,
filename
));
let
pack
=
await
mongodb
.
Packages
.
findOne
({
_id
:
toObjectID
(
ctx
.
params
.
id
)});
if
(
!
pack
)
{
return
ctx
.
throw
(
400
,
'
pack not exists
'
);
}
return
await
new
Promise
((
resolve
,
reject
)
=>
{
file
.
pipe
(
archive
);
file
.
on
(
'
close
'
,
async
()
=>
{
try
{
pack
!
.
status
=
'
uploading
'
;
await
pack
!
.
save
();
resolve
(
pack
!
);
// 上传完, 打包
const
bundled
=
await
bundle
(
filename
);
Object
.
assign
(
pack
,
bundled
);
pack
!
.
status
=
'
uploaded
'
;
await
mongodb
.
Packages
.
update
({
id
:
pack
!
.
id
},
{
$set
:
{
status
:
'
deprecated
'
}},
{
multi
:
true
});
await
pack
!
.
save
();
// 打包完,上传阿里云
}
catch
(
e
)
{
pack
!
.
status
=
'
failed
'
;
await
pack
!
.
save
();
console
.
log
(
e
);
}
});
file
.
on
(
'
error
'
,
async
(
error
)
=>
{
pack
!
.
status
=
'
failed
'
;
await
pack
!
.
save
();
reject
(
error
);
});
});
}));
}
catch
(
err
)
{
ctx
.
throw
(
403
,
err
);
}
};
const
uploadPackageUrl
=
async
(
ctx
:
Context
)
=>
{
if
(
!
ctx
.
request
.
body
.
url
)
{
ctx
.
throw
(
400
,
'
params error
'
);
}
// testUrl: https://r.my-card.in/release/dist/0c16a3ecb115fd7cf575ccdd64f62a8f3edc635b087950e4ed4f3f781972bbfd.tar.gz
const
downloader
=
new
Aria2
;
let
pack
=
await
mongodb
.
Packages
.
findOne
({
_id
:
toObjectID
(
ctx
.
request
.
body
.
_id
)});
if
(
!
pack
)
{
return
ctx
.
throw
(
400
,
'
pack not exists
'
);
}
await
downloader
.
open
();
downloader
.
onDownloadStart
=
async
()
=>
{
pack
!
.
status
=
'
uploading
'
;
await
pack
!
.
save
();
};
downloader
.
onDownloadComplete
=
async
(
m
)
=>
{
const
{
files
}
=
await
downloader
.
send
(
'
tellStatus
'
,
m
.
gid
);
const
[
file
]
=
files
;
try
{
await
checkPackage
(
file
);
// 打包
const
bundled
=
await
bundle
(
path
.
basename
(
file
.
path
));
// 打包完, 上传阿里云
Object
.
assign
(
pack
,
bundled
);
pack
!
.
status
=
'
uploaded
'
;
await
mongodb
.
Packages
.
update
({
id
:
pack
!
.
id
},
{
$set
:
{
status
:
'
deprecated
'
}},
{
multi
:
true
});
await
pack
!
.
save
();
}
catch
(
e
)
{
pack
!
.
status
=
'
failed
'
;
await
pack
!
.
save
();
}
};
downloader
.
onDownloadError
=
async
(
err
)
=>
{
// console.log(await downloader.send('tellStatus', err.gid))
pack
!
.
status
=
'
failed
'
;
await
pack
!
.
save
();
console
.
log
(
err
);
};
ctx
.
body
=
await
new
Promise
((
resolve
,
reject
)
=>
{
downloader
.
onmessage
=
m
=>
{
if
(
m
[
'
error
'
])
{
reject
(
m
[
'
error
'
]);
}
else
{
resolve
(
m
);
}
};
downloader
.
send
(
'
addUri
'
,
[
ctx
.
request
.
body
.
url
],
{
dir
:
config
.
upload_path
});
});
};
router
.
post
(
'
/v1/upload/image
'
,
UploadImage
);
router
.
post
(
'
/v1/upload/package/:id
'
,
UploadPackage
);
router
.
post
(
'
/v1/upload/packageUrl
'
,
uploadPackageUrl
);
export
default
router
;
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