Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
imghost
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
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
Janne Mareike Koschinski
imghost
Commits
088d5d4c
Commit
088d5d4c
authored
7 years ago
by
Janne Mareike Koschinski
Browse files
Options
Downloads
Patches
Plain Diff
Improved upload API
parent
023dd900
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
page_upload.go
+40
-57
40 additions, 57 deletions
page_upload.go
templates/upload.html
+13
-8
13 additions, 8 deletions
templates/upload.html
with
53 additions
and
65 deletions
page_upload.go
+
40
−
57
View file @
088d5d4c
...
...
@@ -92,65 +92,50 @@ func pageUpload(ctx PageContext) http.Handler {
}
}
var
images
[]
Image
var
ids
[]
string
m
:=
r
.
MultipartForm
files
:=
m
.
File
[
"file"
]
for
_
,
header
:=
range
files
{
file
,
err
:=
header
.
Open
()
if
err
!=
nil
{
if
err
=
returnJson
(
w
,
[]
Result
{{
Success
:
false
,
Errors
:
[]
string
{
err
.
Error
()},
}});
err
!=
nil
{
panic
(
err
)
}
return
file
,
header
,
err
:=
r
.
FormFile
(
"file"
)
if
err
!=
nil
{
if
err
=
returnJson
(
w
,
[]
Result
{{
Success
:
false
,
Errors
:
[]
string
{
err
.
Error
()},
}});
err
!=
nil
{
panic
(
err
)
}
image
,
err
:=
createImage
(
ctx
.
Config
,
file
,
header
)
if
err
!=
nil
{
if
err
=
re
turnJson
(
w
,
[]
Result
{{
Success
:
false
,
Errors
:
[]
string
{
err
.
Error
()},
}});
err
!=
nil
{
panic
(
err
)
}
return
return
}
image
,
err
:
=
c
re
ateImage
(
ctx
.
Config
,
file
,
header
)
if
err
!=
nil
{
if
err
=
returnJson
(
w
,
[]
Result
{{
Success
:
false
,
Errors
:
[]
string
{
err
.
Error
()},
}
});
err
!=
nil
{
panic
(
err
)
}
images
=
append
(
images
,
image
)
ids
=
append
(
ids
,
image
.
Id
)
return
}
pubsub
:=
ctx
.
Redis
.
Subscribe
(
ctx
.
Config
.
ResultChannel
)
waiting
:=
make
(
map
[
string
]
bool
)
for
_
,
image
:=
range
images
{
_
,
err
=
ctx
.
Database
.
Exec
(
"INSERT INTO images (id, owner, created_at, original_name, type) VALUES ($1, $2, $3, $4, $5)"
,
image
.
Id
,
user
.
Id
,
image
.
CreatedAt
,
image
.
OriginalName
,
image
.
MimeType
)
if
err
!=
nil
{
panic
(
err
)
}
_
,
err
=
ctx
.
Database
.
Exec
(
"INSERT INTO images (id, owner, created_at, original_name, type) VALUES ($1, $2, $3, $4, $5)"
,
image
.
Id
,
user
.
Id
,
image
.
CreatedAt
,
image
.
OriginalName
,
image
.
MimeType
)
if
err
!=
nil
{
panic
(
err
)
}
data
,
err
:=
json
.
Marshal
(
image
)
if
err
!=
nil
{
if
err
=
returnJson
(
w
,
[]
Result
{{
Success
:
false
,
Errors
:
[]
string
{
err
.
Error
()},
}});
err
!=
nil
{
panic
(
err
)
}
return
data
,
err
:=
json
.
Marshal
(
image
)
if
err
!=
nil
{
if
err
=
returnJson
(
w
,
[]
Result
{{
Success
:
false
,
Errors
:
[]
string
{
err
.
Error
()},
}});
err
!=
nil
{
panic
(
err
)
}
fmt
.
Printf
(
"Created task %s at %d
\n
"
,
image
.
Id
,
time
.
Now
()
.
Unix
())
ctx
.
Redis
.
RPush
(
fmt
.
Sprintf
(
"queue:%s"
,
ctx
.
Config
.
ImageQueue
),
data
)
fmt
.
Printf
(
"Submitted task %s at %d
\n
"
,
image
.
Id
,
time
.
Now
()
.
Unix
())
waiting
[
image
.
Id
]
=
true
return
}
var
results
[]
Result
for
len
(
waiting
)
!=
0
{
fmt
.
Printf
(
"Created task %s at %d
\n
"
,
image
.
Id
,
time
.
Now
()
.
Unix
())
ctx
.
Redis
.
RPush
(
fmt
.
Sprintf
(
"queue:%s"
,
ctx
.
Config
.
ImageQueue
),
data
)
fmt
.
Printf
(
"Submitted task %s at %d
\n
"
,
image
.
Id
,
time
.
Now
()
.
Unix
())
waiting
:=
true
for
waiting
{
message
,
err
:=
pubsub
.
ReceiveMessage
()
if
err
!=
nil
{
if
err
=
returnJson
(
w
,
[]
Result
{{
...
...
@@ -176,16 +161,14 @@ func pageUpload(ctx PageContext) http.Handler {
fmt
.
Printf
(
"Returned task %s at %d
\n
"
,
result
.
Id
,
time
.
Now
()
.
Unix
())
if
_
,
ok
:=
waiting
[
result
.
Id
];
ok
{
delete
(
waiting
,
result
.
Id
)
if
result
.
Id
==
image
.
Id
{
waiting
=
false
results
=
append
(
results
,
result
)
if
err
=
returnJson
(
w
,
result
);
err
!=
nil
{
panic
(
err
)
}
}
}
if
err
=
returnJson
(
w
,
results
);
err
!=
nil
{
panic
(
err
)
}
return
}
else
{
user
:=
parseUser
(
r
)
...
...
This diff is collapsed.
Click to expand it.
templates/upload.html
+
13
−
8
View file @
088d5d4c
...
...
@@ -30,6 +30,17 @@
<div
class=
"uploading images"
></div>
</div>
<script>
function
postData
(
url
,
data
)
{
return
fetch
(
url
,
{
body
:
data
,
cache
:
'
no-cache
'
,
credentials
:
'
same-origin
'
,
method
:
'
POST
'
,
mode
:
'
cors
'
,
redirect
:
'
follow
'
}).
then
(
response
=>
response
.
json
())
}
const
form
=
document
.
querySelector
(
"
form.upload
"
);
const
element
=
document
.
querySelector
(
"
form.upload input[type=file]
"
);
const
results
=
document
.
querySelector
(
"
.uploading.images
"
);
...
...
@@ -46,15 +57,9 @@
results
.
appendChild
(
node
);
const
data
=
new
FormData
();
data
.
append
(
"
file
"
,
file
);
data
.
append
(
"
file
"
,
file
,
file
.
name
);
fetch
(
"
/upload
"
,
{
method
:
"
POST
"
,
credentials
:
"
same-origin
"
,
body
:
data
}).
then
((
response
)
=>
{
return
response
.
json
()
}).
then
((
json
)
=>
{
postData
(
"
/upload/
"
,
data
).
then
((
json
)
=>
{
const
text
=
document
.
createElement
(
"
pre
"
);
text
.
innerText
=
JSON
.
stringify
(
json
);
node
.
appendChild
(
text
);
...
...
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