Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
pubsweet
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
Container Registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Giorgio Sironi
pubsweet
Commits
ce80a99d
Commit
ce80a99d
authored
7 years ago
by
Alf Eaton
Browse files
Options
Downloads
Patches
Plain Diff
Apply linter
parent
a35cb761
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
packages/components/packages/InkBackend/InkBackend.js
+115
-91
115 additions, 91 deletions
packages/components/packages/InkBackend/InkBackend.js
with
115 additions
and
91 deletions
packages/components/packages/InkBackend/InkBackend.js
+
115
−
91
View file @
ce80a99d
...
@@ -12,7 +12,7 @@ const Pusher = require('pusher-js')
...
@@ -12,7 +12,7 @@ const Pusher = require('pusher-js')
const
inkConfig
=
config
.
get
(
'
pubsweet-component-ink-backend
'
)
const
inkConfig
=
config
.
get
(
'
pubsweet-component-ink-backend
'
)
// Generate the absolute URL
// Generate the absolute URL
const
inkUrl
=
path
=>
inkConfig
.
inkEndpoint
+
'
api/
'
+
path
const
inkUrl
=
path
=>
`
${
inkConfig
.
inkEndpoint
}
api/
${
path
}
`
// Connect to the INK Pusher/Slanger endpoint
// Connect to the INK Pusher/Slanger endpoint
const
connectToPusher
=
({
appKey
,
...
options
})
=>
new
Pusher
(
appKey
,
options
)
const
connectToPusher
=
({
appKey
,
...
options
})
=>
new
Pusher
(
appKey
,
options
)
...
@@ -20,63 +20,67 @@ const connectToPusher = ({ appKey, ...options }) => new Pusher(appKey, options)
...
@@ -20,63 +20,67 @@ const connectToPusher = ({ appKey, ...options }) => new Pusher(appKey, options)
const
pusher
=
connectToPusher
(
inkConfig
.
pusher
)
const
pusher
=
connectToPusher
(
inkConfig
.
pusher
)
// Sign in
// Sign in
const
authorize
=
()
=>
rp
({
const
authorize
=
()
=>
method
:
'
POST
'
,
rp
({
uri
:
inkUrl
(
'
auth/sign_in
'
),
method
:
'
POST
'
,
formData
:
{
uri
:
inkUrl
(
'
auth/sign_in
'
),
email
:
inkConfig
.
email
,
formData
:
{
password
:
inkConfig
.
password
email
:
inkConfig
.
email
,
},
password
:
inkConfig
.
password
,
headers
:
{
},
'
Accept
'
:
'
application/vnd.ink.1
'
headers
:
{
},
Accept
:
'
application/vnd.ink.1
'
,
resolveWithFullResponse
:
true
},
}).
then
(
res
=>
({
resolveWithFullResponse
:
true
,
'
client
'
:
res
.
headers
[
'
client
'
],
}).
then
(
res
=>
({
'
access-token
'
:
res
.
headers
[
'
access-token
'
]
client
:
res
.
headers
.
client
,
}))
'
access-token
'
:
res
.
headers
[
'
access-token
'
],
}))
// Upload file to INK and execute the recipe
// Upload file to INK and execute the recipe
const
upload
=
(
recipeId
,
inputFile
,
auth
)
=>
rp
({
const
upload
=
(
recipeId
,
inputFile
,
auth
)
=>
method
:
'
POST
'
,
rp
({
uri
:
inkUrl
(
'
recipes/
'
+
recipeId
+
'
/execute
'
),
method
:
'
POST
'
,
headers
:
{
uri
:
inkUrl
(
`recipes/
${
recipeId
}
/execute`
),
uid
:
inkConfig
.
email
,
headers
:
{
...
auth
uid
:
inkConfig
.
email
,
},
...
auth
,
formData
:
{
},
input_files
:
[
inputFile
]
formData
:
{
},
input_files
:
[
inputFile
],
json
:
true
,
},
timeout
:
60
*
60
*
1000
// 3600 seconds
json
:
true
,
})
timeout
:
60
*
60
*
1000
,
// 3600 seconds
})
// Download the output file
// Download the output file
const
download
=
(
chainId
,
auth
,
outputFileName
)
=>
rp
({
const
download
=
(
chainId
,
auth
,
outputFileName
)
=>
uri
:
inkUrl
(
'
process_chains/
'
+
chainId
+
'
/download_output_file
'
),
rp
({
qs
:
{
uri
:
inkUrl
(
`process_chains/
${
chainId
}
/download_output_file`
),
relative_path
:
outputFileName
qs
:
{
},
relative_path
:
outputFileName
,
headers
:
{
},
uid
:
inkConfig
.
email
,
headers
:
{
...
auth
uid
:
inkConfig
.
email
,
}
...
auth
,
})
},
})
// Find the ID of a recipe by name
// Find the ID of a recipe by name
const
findRecipeId
=
(
name
=
'
Editoria Typescript
'
,
auth
)
=>
rp
({
const
findRecipeId
=
(
name
=
'
Editoria Typescript
'
,
auth
)
=>
method
:
'
GET
'
,
rp
({
uri
:
inkUrl
(
'
recipes
'
),
method
:
'
GET
'
,
headers
:
{
uri
:
inkUrl
(
'
recipes
'
),
uid
:
inkConfig
.
email
,
headers
:
{
...
auth
uid
:
inkConfig
.
email
,
},
...
auth
,
json
:
true
},
}).
then
(
data
=>
{
json
:
true
,
const
recipe
=
data
.
recipes
.
find
(
recipe
=>
recipe
.
name
===
name
)
}).
then
(
data
=>
{
const
recipe
=
data
.
recipes
.
find
(
recipe
=>
recipe
.
name
===
name
)
return
recipe
?
recipe
.
id
:
null
})
return
recipe
?
recipe
.
id
:
null
})
const
process
=
async
(
inputFile
,
options
)
=>
{
const
process
=
async
(
inputFile
,
options
)
=>
{
const
auth
=
await
authorize
().
catch
(
err
=>
{
const
auth
=
await
authorize
().
catch
(
err
=>
{
...
@@ -85,7 +89,9 @@ const process = async (inputFile, options) => {
...
@@ -85,7 +89,9 @@ const process = async (inputFile, options) => {
})
})
// either use the recipe id from the configuration or search for it by name
// either use the recipe id from the configuration or search for it by name
const
recipeId
=
inkConfig
.
recipes
[
options
.
recipe
]
||
await
findRecipeId
(
options
.
recipe
,
auth
)
const
recipeId
=
inkConfig
.
recipes
[
options
.
recipe
]
||
(
await
findRecipeId
(
options
.
recipe
,
auth
))
if
(
!
recipeId
)
throw
new
Error
(
'
Unknown recipe
'
)
if
(
!
recipeId
)
throw
new
Error
(
'
Unknown recipe
'
)
const
response
=
await
upload
(
recipeId
,
inputFile
,
auth
).
catch
(
err
=>
{
const
response
=
await
upload
(
recipeId
,
inputFile
,
auth
).
catch
(
err
=>
{
...
@@ -115,23 +121,36 @@ const process = async (inputFile, options) => {
...
@@ -115,23 +121,36 @@ const process = async (inputFile, options) => {
const
manifest
=
chain
.
input_file_manifest
const
manifest
=
chain
.
input_file_manifest
if
(
manifest
.
length
===
0
)
{
if
(
manifest
.
length
===
0
)
{
reject
(
new
Error
(
'
The INK server gave a malformed response (no input files in the process chain)
'
))
reject
(
new
Error
(
'
The INK server gave a malformed response (no input files in the process chain)
'
,
),
)
}
}
// backwards compatibility
// backwards compatibility
if
(
!
options
.
outputFileName
)
{
if
(
!
options
.
outputFileName
)
{
options
.
outputFileName
=
path
.
basename
(
manifest
[
0
].
path
,
'
.docx
'
)
+
'
.html
'
options
.
outputFileName
=
`
${
path
.
basename
(
manifest
[
0
].
path
,
'
.docx
'
,
)}
.html`
}
}
// download the output file
// download the output file
logger
.
info
(
`Downloading output file
${
options
.
outputFileName
}
from chain
${
chain
.
id
}
`
)
logger
.
info
(
`Downloading output file
${
options
.
outputFileName
}
from chain
${
download
(
chain
.
id
,
auth
,
options
.
outputFileName
).
then
(
result
=>
{
chain
.
id
resolve
(
result
)
}
`
,
}).
catch
(
error
=>
{
)
logger
.
error
(
'
Error downloading from INK:
'
,
error
.
message
)
reject
(
error
)
download
(
chain
.
id
,
auth
,
options
.
outputFileName
)
})
.
then
(
result
=>
{
resolve
(
result
)
})
.
catch
(
error
=>
{
logger
.
error
(
'
Error downloading from INK:
'
,
error
.
message
)
reject
(
error
)
})
}
}
// handle "processing completed" events on this channel
// handle "processing completed" events on this channel
...
@@ -145,41 +164,46 @@ const process = async (inputFile, options) => {
...
@@ -145,41 +164,46 @@ const process = async (inputFile, options) => {
})
})
}
}
const
InkBackend
=
function
(
app
)
{
const
InkBackend
=
app
=>
{
// TODO: authentication on this route
// TODO: authentication on this route
app
.
use
(
'
/api/ink
'
,
(
req
,
res
,
next
)
=>
{
app
.
use
(
'
/api/ink
'
,
(
req
,
res
,
next
)
=>
{
const
fileStream
=
new
Busboy
({
headers
:
req
.
headers
})
const
fileStream
=
new
Busboy
({
headers
:
req
.
headers
})
fileStream
.
on
(
'
file
'
,
(
fieldname
,
file
,
filename
,
encoding
,
contentType
)
=>
{
fileStream
.
on
(
const
stream
=
temp
.
createWriteStream
()
'
file
'
,
(
fieldname
,
file
,
filename
,
encoding
,
contentType
)
=>
{
stream
.
on
(
'
finish
'
,
()
=>
{
const
stream
=
temp
.
createWriteStream
()
const
inputFile
=
{
value
:
fs
.
createReadStream
(
stream
.
path
),
stream
.
on
(
'
finish
'
,
()
=>
{
options
:
{
filename
,
contentType
}
const
inputFile
=
{
}
value
:
fs
.
createReadStream
(
stream
.
path
),
options
:
{
filename
,
contentType
},
logger
.
info
(
`Uploading file to INK for processing`
)
}
process
(
inputFile
,
req
.
query
).
then
(
converted
=>
{
logger
.
info
(
`Uploading file to INK for processing`
)
res
.
json
({
converted
})
process
(
inputFile
,
req
.
query
)
// clean up temp file
.
then
(
converted
=>
{
fs
.
unlink
(
stream
.
path
,
()
=>
{
res
.
json
({
converted
})
logger
.
info
(
'
Deleted temporary file
'
,
stream
.
path
)
})
// clean up temp file
}).
catch
(
err
=>
{
fs
.
unlink
(
stream
.
path
,
()
=>
{
logger
.
error
(
'
ERROR CONVERTING WITH INK:
'
,
err
.
message
)
logger
.
info
(
'
Deleted temporary file
'
,
stream
.
path
)
next
(
err
)
})
})
.
catch
(
err
=>
{
logger
.
error
(
'
ERROR CONVERTING WITH INK:
'
,
err
.
message
)
next
(
err
)
})
})
})
})
file
.
pipe
(
stream
)
file
.
pipe
(
stream
)
file
.
on
(
'
end
'
,
()
=>
{
file
.
on
(
'
end
'
,
()
=>
{
stream
.
end
()
stream
.
end
()
})
})
})
},
)
fileStream
.
on
(
'
error
'
,
err
=>
{
fileStream
.
on
(
'
error
'
,
err
=>
{
logger
.
error
(
err
)
logger
.
error
(
err
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
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