Skip to content

Commit

Permalink
uploading-downloading seems to work now
Browse files Browse the repository at this point in the history
  • Loading branch information
or-else committed Jun 9, 2018
1 parent bfb5331 commit e0cfa60
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 34 deletions.
54 changes: 32 additions & 22 deletions server/db/mysql/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,12 +342,12 @@ func (a *adapter) CreateDb(reset bool) error {
// Deletion log
if _, err = tx.Exec(
`CREATE TABLE dellog(
id INT NOT NULL AUTO_INCREMENT,
topic VARCHAR(25) NOT NULL,
deletedfor BIGINT NOT NULL DEFAULT 0,
delid INT NOT NULL,
low INT NOT NULL,
hi INT NOT NULL,
id INT NOT NULL AUTO_INCREMENT,
topic VARCHAR(25) NOT NULL,
deletedfor BIGINT NOT NULL DEFAULT 0,
delid INT NOT NULL,
low INT NOT NULL,
hi INT NOT NULL,
PRIMARY KEY(id),
FOREIGN KEY(topic) REFERENCES topics(name),
UNIQUE INDEX dellog_topic_delid_deletedfor(topic,delid,deletedfor),
Expand All @@ -360,13 +360,13 @@ func (a *adapter) CreateDb(reset bool) error {
// User credentials
if _, err = tx.Exec(
`CREATE TABLE credentials(
id INT NOT NULL AUTO_INCREMENT,
createdat DATETIME(3) NOT NULL,
updatedat DATETIME(3) NOT NULL,
method VARCHAR(16) NOT NULL,
id INT NOT NULL AUTO_INCREMENT,
createdat DATETIME(3) NOT NULL,
updatedat DATETIME(3) NOT NULL,
method VARCHAR(16) NOT NULL,
value VARCHAR(128) NOT NULL,
synthetic VARCHAR(192) NOT NULL,
userid BIGINT NOT NULL,
userid BIGINT NOT NULL,
resp VARCHAR(255),
done TINYINT NOT NULL DEFAULT 0,
retries INT NOT NULL DEFAULT 0,
Expand All @@ -380,14 +380,14 @@ func (a *adapter) CreateDb(reset bool) error {
// Records of uploaded files.
if _, err = tx.Exec(
`CREATE TABLE fileuploads(
id INT NOT NULL AUTO_INCREMENT,
id BIGINT NOT NULL,
createdat DATETIME(3) NOT NULL,
updatedat DATETIME(3) NOT NULL,
userid BIGINT NOT NULL,
status INT NOT NULL,
mimetype VARCHAR(255) NOT NULL,
size BIGINT NOT NULL,
location VARCHAR(255) NOT NULL,
location VARCHAR(2048) NOT NULL,
PRIMARY KEY(id),
FOREIGN KEY(userid) REFERENCES users(id)
)`); err != nil {
Expand Down Expand Up @@ -1734,25 +1734,27 @@ func (a *adapter) CredGet(uid t.Uid, method string) ([]*t.Credential, error) {
// FileStartUpload initializes a file upload
func (a *adapter) FileStartUpload(fd *t.FileDef) error {
_, err := a.db.Exec("INSERT INTO fileuploads(id,createdat,updatedat,userid,status,mimetype,size,location)"+
" VALUES(?,?,?,?,?,?,?,?)", fd.Id, fd.CreatedAt, fd.UpdatedAt, fd.User, fd.Status, fd.MimeType,
fd.Size, fd.Location)
" VALUES(?,?,?,?,?,?,?,?)",
store.DecodeUid(fd.Uid()), fd.CreatedAt, fd.UpdatedAt,
store.DecodeUid(t.ParseUid(fd.User)), fd.Status, fd.MimeType, fd.Size, fd.Location)
return err
}

// FileFinishUpload markes file upload as completed, successfully or otherwise
func (a *adapter) FileFinishUpload(fid string, status int) (*t.FileDef, error) {
id := t.ParseUid(fid)
if id.IsZero() {
return nil, t.ErrMalformed
}

fd, err := a.FileGet(fid)
if err != nil {
return nil, err
}
if fd == nil {
return nil, t.ErrNotFound
}
id := t.ParseUid(fid)
if id.IsZero() {
return nil, t.ErrMalformed
}
_, err = a.db.Exec("UPDATE fileuploads SET status=? WHERE id=?", status, id)
_, err = a.db.Exec("UPDATE fileuploads SET status=? WHERE id=?", status, store.DecodeUid(id))
if err == nil {
fd.Status = status
} else {
Expand All @@ -1761,7 +1763,8 @@ func (a *adapter) FileFinishUpload(fid string, status int) (*t.FileDef, error) {
return fd, err
}

// FilesForUser returns all file records for a given user.
// FilesForUser returns all file records for a given user. Query is currently ignored.
// FIXME: use opts.
func (a *adapter) FilesForUser(uid t.Uid, opts *t.QueryOpt) ([]t.FileDef, error) {
rows, err := a.db.Queryx("SELECT id,createdat,updatedat,status,mimetype,size,location "+
"FROM fileuploads WHERE userid=?", store.DecodeUid(uid))
Expand All @@ -1777,6 +1780,7 @@ func (a *adapter) FilesForUser(uid t.Uid, opts *t.QueryOpt) ([]t.FileDef, error)
if err = rows.StructScan(&fd); err != nil {
break
}
fd.Id = encodeString(fd.Id).String()
fd.User = user
result = append(result, fd)
}
Expand All @@ -1793,12 +1797,18 @@ func (a *adapter) FileGet(fid string) (*t.FileDef, error) {
}

var fd t.FileDef
err := a.db.Get(&fd, "SELECT id,createdat,updatedat,status,mimetype,size,location "+
err := a.db.Get(&fd, "SELECT id,createdat,updatedat,userid AS user,status,mimetype,size,location "+
"FROM fileuploads WHERE id=?", store.DecodeUid(id))
if err == sql.ErrNoRows {
return nil, nil
}
if err != nil {
return nil, err
}

fd.Id = encodeString(fd.Id).String()
fd.User = encodeString(fd.User).String()

return &fd, nil

}
Expand Down
15 changes: 8 additions & 7 deletions server/db/mysql/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,10 @@ CREATE TABLE messages(

# Deletion log
CREATE TABLE dellog(
id INT NOT NULL AUTO_INCREMENT,
topic VARCHAR(25) NOT NULL,
deletedfor BIGINT NOT NULL DEFAULT 0,
delid INT NOT NULL,
id INT NOT NULL AUTO_INCREMENT,
topic VARCHAR(25) NOT NULL,
deletedfor BIGINT NOT NULL DEFAULT 0,
delid INT NOT NULL,
low INT NOT NULL,
hi INT NOT NULL,

Expand All @@ -162,7 +162,7 @@ CREATE TABLE dellog(

# User credentials
CREATE TABLE credentials(
id INT NOT NULL AUTO_INCREMENT,
id INT NOT NULL AUTO_INCREMENT,
createdat DATETIME(3) NOT NULL,
updatedat DATETIME(3) NOT NULL,
method VARCHAR(16) NOT NULL,
Expand All @@ -180,14 +180,15 @@ CREATE TABLE credentials(

# Records of uploaded files. Files themselves are stored elsewhere.
CREATE TABLE fileuploads(
id INT NOT NULL AUTO_INCREMENT,
id BIGINT NOT NULL,
createdat DATETIME(3) NOT NULL,
updatedat DATETIME(3) NOT NULL,
userid BIGINT NOT NULL,
status INT NOT NULL,
mimetype VARCHAR(255) NOT NULL,
size BIGINT NOT NULL,
location VARCHAR(255) NOT NULL,
location VARCHAR(2048) NOT NULL,

PRIMARY KEY(id),
FOREIGN KEY(userid) REFERENCES users(id)
)
14 changes: 12 additions & 2 deletions server/hdl_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func largeFileServe(wrt http.ResponseWriter, req *http.Request) {
dir, fname := path.Split(path.Clean(req.URL.Path))

// Check path validity
if dir != "/v0/file/s" {
if dir != "/v0/file/s/" {
writeHttpResponse(ErrNotFound("", "", now))
return
}
Expand All @@ -84,15 +84,21 @@ func largeFileServe(wrt http.ResponseWriter, req *http.Request) {
parts := strings.Split(fname, ".")
fname = parts[0]

log.Println("Fetching file record for", fname)

fd, err := store.Files.Get(fname)
if err != nil {
writeHttpResponse(decodeStoreError(err, "", "", now, nil))
return
}
if fd == nil {
log.Println("File record not found", fname)
writeHttpResponse(ErrNotFound("", "", now))
return
}

// FIXME: The following code is dependent on storage method.
log.Println("Opening file", fd.Location)
file, err := os.Open(fd.Location)
if err != nil {
writeHttpResponse(nil)
Expand Down Expand Up @@ -193,7 +199,11 @@ func largeFileUpload(wrt http.ResponseWriter, req *http.Request) {
}
defer outfile.Close()

store.Files.StartUpload(&fdef)
if err = store.Files.StartUpload(&fdef); err != nil {
log.Println("Failed to create file record", fdef.Id, err)
writeHttpResponse(decodeStoreError(err, "", "", now, nil))
return
}

_, err = io.Copy(outfile, file)
log.Println("Finished upload", fdef.Location)
Expand Down
14 changes: 13 additions & 1 deletion server/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,13 @@ func tlsRedirect(toPort string) http.HandlerFunc {

// Get API key from an HTTP request.
func getAPIKey(req *http.Request) string {
apikey := req.FormValue("apikey")
// Check URL query parameters.
apikey := req.URL.Query().Get("apikey")
// Check form values.
if apikey == "" {
apikey = req.FormValue("apikey")
}
// Check headers.
if apikey == "" {
apikey = req.Header.Get("X-Tinode-APIKey")
}
Expand All @@ -229,9 +235,15 @@ func getAPIKey(req *http.Request) string {

// Get authorization credentials from an HTTP request.
func getHttpAuth(req *http.Request) (string, string) {
// Check URL query parameters.
if auth := req.URL.Query().Get("auth"); auth != "" {
return auth, req.URL.Query().Get("secret")
}
// Check form values.
if auth := req.FormValue("auth"); auth != "" {
return auth, req.FormValue("secret")
}
// Check Authorization header.
if parts := strings.Split(req.Header.Get("Authorization"), " "); len(parts) == 2 {
return parts[0], parts[1]
}
Expand Down
4 changes: 2 additions & 2 deletions server/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -629,10 +629,10 @@ func (FileMapper) GetForUser(uid types.Uid) ([]types.FileDef, error) {

// Get fetches a file record for a unique file id.
func (FileMapper) Get(fid string) (*types.FileDef, error) {
return nil, nil
return adp.FileGet(fid)
}

// Delete file record by unique ID.
func (FileMapper) Delete(fid string) error {
return nil
return adp.FileDelete(fid)
}

0 comments on commit e0cfa60

Please sign in to comment.