Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
api
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
whats-this
api
Commits
f7313414
Commit
f7313414
authored
5 years ago
by
Dean
Browse files
Options
Downloads
Patches
Plain Diff
add sha256_hash col, change md5_hash to be bytea
parent
bf065271
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
lib/db/queries.go
+2
-1
2 additions, 1 deletion
lib/db/queries.go
lib/db/sql.go
+2
-2
2 additions, 2 deletions
lib/db/sql.go
lib/routes/uploadpomf.go
+14
-11
14 additions, 11 deletions
lib/routes/uploadpomf.go
with
18 additions
and
14 deletions
lib/db/queries.go
+
2
−
1
View file @
f7313414
...
...
@@ -61,7 +61,7 @@ func InsertShortURL(bucket, key, destURL string, associatedUser *string) error {
}
// InsertFile inserts a file object into the database.
func
InsertFile
(
bucket
,
key
,
ext
,
contentType
string
,
contentLength
int64
,
md5Hash
s
tring
,
associatedUser
*
string
)
error
{
func
InsertFile
(
bucket
,
key
,
ext
,
contentType
string
,
contentLength
int64
,
md5Hash
,
s
ha256Hash
[]
byte
,
associatedUser
*
string
)
error
{
if
!
strings
.
HasPrefix
(
key
,
"/"
)
{
key
=
"/"
+
key
}
...
...
@@ -73,6 +73,7 @@ func InsertFile(bucket, key, ext, contentType string, contentLength int64, md5Ha
contentType
,
contentLength
,
md5Hash
,
sha256Hash
,
associatedUser
)
if
err
!=
nil
{
return
err
...
...
This diff is collapsed.
Click to expand it.
lib/db/sql.go
+
2
−
2
View file @
f7313414
...
...
@@ -36,9 +36,9 @@ VALUES
var
insertFile
=
`
INSERT INTO
objects (bucket_key, bucket, key, random_key, dir, content_type, content_length, md5_hash, associated_user)
objects (bucket_key, bucket, key, random_key, dir, content_type, content_length, md5_hash,
sha256_hash,
associated_user)
VALUES
($1, $2, $3, $4, '/', $5, $6, $7, $8)
($1, $2, $3, $4, '/', $5, $6, $7, $8
, $9
)
`
var
selectUserByUsernameOrEmail
=
`
...
...
This diff is collapsed.
Click to expand it.
lib/routes/uploadpomf.go
+
14
−
11
View file @
f7313414
...
...
@@ -2,6 +2,7 @@ package routes
import
(
"crypto/md5"
"crypto/sha256"
"encoding/hex"
"io"
"net/http"
...
...
@@ -22,7 +23,7 @@ import (
"github.com/spf13/viper"
)
// Maximum memory per upload.
// Maximum memory per upload
before using temporary files
.
const
maxMemory
=
1000
*
1000
*
50
// 50 MB
// File field name for multipart/form-data.
...
...
@@ -55,7 +56,7 @@ type fileResponse struct {
Success
bool
`json:"success"`
StatusCode
int
`json:"errorcode,omitempty"`
Description
string
`json:"description,omitempty"`
Hash
string
`json:"hash,omitempty"`
Hash
string
`json:"hash,omitempty"`
// MD5 hash, not SHA256 hash
Name
string
`json:"name,omitempty"`
URL
string
`json:"url,omitempty"`
Size
int64
`json:"size,omitempty"`
...
...
@@ -168,12 +169,13 @@ func UploadPomf(associateObjectsWithUser bool) func(http.ResponseWriter, *http.R
continue
}
// Write file to MD5 and to temp file
hash
:=
md5
.
New
()
// Write file to MD5 and SHA256 hashers and to temp file
md5Hash
:=
md5
.
New
()
sha256Hash
:=
sha256
.
New
()
tempPath
:=
filepath
.
Join
(
viper
.
GetString
(
"pomf.tempLocation"
),
key
+
ext
)
tempFile
,
err
:=
os
.
Create
(
tempPath
)
if
err
!=
nil
{
log
.
Error
()
.
Err
(
err
)
.
Msg
(
"failed to create destination file"
)
log
.
Error
()
.
Err
(
err
)
.
Msg
(
"failed to create
temporary
destination file"
)
if
len
(
files
)
==
1
{
panic
(
apierrors
.
InternalServerError
)
}
...
...
@@ -185,11 +187,11 @@ func UploadPomf(associateObjectsWithUser bool) func(http.ResponseWriter, *http.R
})
continue
}
writer
:=
io
.
MultiWriter
(
h
ash
,
tempFile
)
writer
:=
io
.
MultiWriter
(
md5Hash
,
sha256H
ash
,
tempFile
)
_
,
err
=
io
.
Copy
(
writer
,
f
)
tempFile
.
Close
()
if
err
!=
nil
{
log
.
Error
()
.
Err
(
err
)
.
Msg
(
"failed to write to
MD5
hasher and temporary path"
)
log
.
Error
()
.
Err
(
err
)
.
Msg
(
"failed to write to hasher
s
and temporary path"
)
err
=
os
.
Remove
(
tempPath
)
if
err
!=
nil
{
log
.
Error
()
.
Err
(
err
)
.
Msg
(
"failed to delete temporary file after error"
)
...
...
@@ -218,15 +220,16 @@ func UploadPomf(associateObjectsWithUser bool) func(http.ResponseWriter, *http.R
panic
(
apierrors
.
InternalServerError
)
}
// Get MD5 hash digest
md5Hash
:=
hex
.
EncodeToString
(
hash
.
Sum
(
nil
))
// Get checksums
md5Bytes
:=
md5Hash
.
Sum
(
nil
)
sha256Bytes
:=
sha256Hash
.
Sum
(
nil
)
// Insert object into database
var
associatedUser
*
string
if
associateObjectsWithUser
{
associatedUser
=
&
user
.
ID
}
err
=
db
.
InsertFile
(
bucket
,
key
,
ext
,
contentType
,
file
.
Size
,
md5
Hash
,
associatedUser
)
err
=
db
.
InsertFile
(
bucket
,
key
,
ext
,
contentType
,
file
.
Size
,
md5
Bytes
,
sha256Bytes
,
associatedUser
)
if
err
!=
nil
{
log
.
Error
()
.
Err
(
err
)
.
Msg
(
"failed to create DB object for file upload"
)
err
=
os
.
Remove
(
destPath
)
...
...
@@ -247,7 +250,7 @@ func UploadPomf(associateObjectsWithUser bool) func(http.ResponseWriter, *http.R
fileResponses
=
append
(
fileResponses
,
fileResponse
{
Success
:
true
,
Hash
:
md5Hash
,
Hash
:
hex
.
EncodeToString
(
md5Bytes
)
,
Name
:
file
.
Filename
,
URL
:
key
+
ext
,
Size
:
file
.
Size
,
...
...
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