Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
Sharkey
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Dima Krasner
Sharkey
Commits
a7e60f80
Unverified
Commit
a7e60f80
authored
6 years ago
by
syuilo
Browse files
Options
Downloads
Patches
Plain Diff
Refactor: Extract downloadTextFile function
parent
3dd5f313
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/misc/download-text-file.ts
+79
-0
79 additions, 0 deletions
src/misc/download-text-file.ts
src/queue/processors/db/import-user-lists.ts
+2
-69
2 additions, 69 deletions
src/queue/processors/db/import-user-lists.ts
with
81 additions
and
69 deletions
src/misc/download-text-file.ts
0 → 100644
+
79
−
0
View file @
a7e60f80
import
*
as
tmp
from
'
tmp
'
;
import
*
as
fs
from
'
fs
'
;
import
*
as
util
from
'
util
'
;
import
chalk
from
'
chalk
'
;
import
*
as
request
from
'
request
'
;
import
Logger
from
'
../services/logger
'
;
import
config
from
'
../config
'
;
const
logger
=
new
Logger
(
'
download-text-file
'
);
export
async
function
downloadTextFile
(
url
:
string
):
Promise
<
string
>
{
// Create temp file
const
[
path
,
cleanup
]
=
await
new
Promise
<
[
string
,
any
]
>
((
res
,
rej
)
=>
{
tmp
.
file
((
e
,
path
,
fd
,
cleanup
)
=>
{
if
(
e
)
return
rej
(
e
);
res
([
path
,
cleanup
]);
});
});
logger
.
info
(
`Temp file is
${
path
}
`
);
// write content at URL to temp file
await
new
Promise
((
res
,
rej
)
=>
{
logger
.
info
(
`Downloading
${
chalk
.
cyan
(
url
)}
...`
);
const
writable
=
fs
.
createWriteStream
(
path
);
writable
.
on
(
'
finish
'
,
()
=>
{
logger
.
succ
(
`Download finished:
${
chalk
.
cyan
(
url
)}
`
);
res
();
});
writable
.
on
(
'
error
'
,
error
=>
{
logger
.
error
(
`Download failed:
${
chalk
.
cyan
(
url
)}
:
${
error
}
`
,
{
url
:
url
,
e
:
error
});
rej
(
error
);
});
const
requestUrl
=
new
URL
(
url
).
pathname
.
match
(
/
[^\u
0021-
\u
00ff
]
/
)
?
encodeURI
(
url
)
:
url
;
const
req
=
request
({
url
:
requestUrl
,
proxy
:
config
.
proxy
,
timeout
:
10
*
1000
,
headers
:
{
'
User-Agent
'
:
config
.
userAgent
}
});
req
.
pipe
(
writable
);
req
.
on
(
'
response
'
,
response
=>
{
if
(
response
.
statusCode
!==
200
)
{
logger
.
error
(
`Got
${
response
.
statusCode
}
(
${
url
}
)`
);
writable
.
close
();
rej
(
response
.
statusCode
);
}
});
req
.
on
(
'
error
'
,
error
=>
{
logger
.
error
(
`Failed to start download:
${
chalk
.
cyan
(
url
)}
:
${
error
}
`
,
{
url
:
url
,
e
:
error
});
writable
.
close
();
rej
(
error
);
});
});
logger
.
succ
(
`Downloaded to:
${
path
}
`
);
const
text
=
await
util
.
promisify
(
fs
.
readFile
)(
path
,
'
utf8
'
);
cleanup
();
return
text
;
}
This diff is collapsed.
Click to expand it.
src/queue/processors/db/import-user-lists.ts
+
2
−
69
View file @
a7e60f80
import
*
as
Bull
from
'
bull
'
;
import
*
as
tmp
from
'
tmp
'
;
import
*
as
fs
from
'
fs
'
;
import
*
as
util
from
'
util
'
;
import
*
as
mongo
from
'
mongodb
'
;
import
*
as
request
from
'
request
'
;
import
{
queueLogger
}
from
'
../../logger
'
;
import
User
from
'
../../../models/user
'
;
import
config
from
'
../../../config
'
;
import
UserList
from
'
../../../models/user-list
'
;
import
DriveFile
from
'
../../../models/drive-file
'
;
import
chalk
from
'
chalk
'
;
import
{
getOriginalUrl
}
from
'
../../../misc/get-drive-file-url
'
;
import
parseAcct
from
'
../../../misc/acct/parse
'
;
import
resolveUser
from
'
../../../remote/resolve-user
'
;
import
{
pushUserToUserList
}
from
'
../../../services/user-list/push
'
;
import
{
downloadTextFile
}
from
'
../../../misc/download-text-file
'
;
const
logger
=
queueLogger
.
createSubLogger
(
'
import-user-lists
'
);
...
...
@@ -31,69 +27,7 @@ export async function importUserLists(job: Bull.Job, done: any): Promise<void> {
const
url
=
getOriginalUrl
(
file
);
// Create temp file
const
[
path
,
cleanup
]
=
await
new
Promise
<
[
string
,
any
]
>
((
res
,
rej
)
=>
{
tmp
.
file
((
e
,
path
,
fd
,
cleanup
)
=>
{
if
(
e
)
return
rej
(
e
);
res
([
path
,
cleanup
]);
});
});
logger
.
info
(
`Temp file is
${
path
}
`
);
// write content at URL to temp file
await
new
Promise
((
res
,
rej
)
=>
{
logger
.
info
(
`Downloading
${
chalk
.
cyan
(
url
)}
...`
);
const
writable
=
fs
.
createWriteStream
(
path
);
writable
.
on
(
'
finish
'
,
()
=>
{
logger
.
succ
(
`Download finished:
${
chalk
.
cyan
(
url
)}
`
);
res
();
});
writable
.
on
(
'
error
'
,
error
=>
{
logger
.
error
(
`Download failed:
${
chalk
.
cyan
(
url
)}
:
${
error
}
`
,
{
url
:
url
,
e
:
error
});
rej
(
error
);
});
const
requestUrl
=
new
URL
(
url
).
pathname
.
match
(
/
[^\u
0021-
\u
00ff
]
/
)
?
encodeURI
(
url
)
:
url
;
const
req
=
request
({
url
:
requestUrl
,
proxy
:
config
.
proxy
,
timeout
:
10
*
1000
,
headers
:
{
'
User-Agent
'
:
config
.
userAgent
}
});
req
.
pipe
(
writable
);
req
.
on
(
'
response
'
,
response
=>
{
if
(
response
.
statusCode
!==
200
)
{
logger
.
error
(
`Got
${
response
.
statusCode
}
(
${
url
}
)`
);
writable
.
close
();
rej
(
response
.
statusCode
);
}
});
req
.
on
(
'
error
'
,
error
=>
{
logger
.
error
(
`Failed to start download:
${
chalk
.
cyan
(
url
)}
:
${
error
}
`
,
{
url
:
url
,
e
:
error
});
writable
.
close
();
rej
(
error
);
});
});
logger
.
succ
(
`Downloaded to:
${
path
}
`
);
const
csv
=
await
util
.
promisify
(
fs
.
readFile
)(
path
,
'
utf8
'
);
const
csv
=
await
downloadTextFile
(
url
);
for
(
const
line
of
csv
.
trim
().
split
(
'
\n
'
))
{
const
listName
=
line
.
split
(
'
,
'
)[
0
].
trim
();
...
...
@@ -132,6 +66,5 @@ export async function importUserLists(job: Bull.Job, done: any): Promise<void> {
}
logger
.
succ
(
'
Imported
'
);
cleanup
();
done
();
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment