Skip to content

Commit

Permalink
Fixed further translation bugs. All things seem to work for JIRA now
Browse files Browse the repository at this point in the history
  • Loading branch information
frikky committed Apr 21, 2024
1 parent 5a7f0a2 commit 221c15a
Show file tree
Hide file tree
Showing 6 changed files with 417 additions and 91 deletions.
4 changes: 2 additions & 2 deletions app_upload/stitcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -844,8 +844,8 @@ func main() {
bucketName = os.Args[5]
}

appname := "shuffle-tools"
appversion := "1.2.0"
appname := "http"
appversion := "1.4.0"
err := deployConfigToBackend(appfolder, appname, appversion)
if err != nil {
log.Printf("[WARNING] Failed uploading config: %s", err)
Expand Down
47 changes: 47 additions & 0 deletions db-connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -8047,6 +8047,18 @@ func GetFile(ctx context.Context, id string) (*File, error) {
nameKey := "Files"

cacheKey := fmt.Sprintf("%s_%s", nameKey, id)
if project.CacheDb {
cache, err := GetCache(ctx, cacheKey)
if err == nil {
cacheData := []byte(cache.([]uint8))
curFile := &File{}
err = json.Unmarshal(cacheData, &curFile)
if err == nil {
return curFile, nil
}
}
}

curFile := &File{}
if project.DbType == "opensearch" {
//log.Printf("GETTING ES USER %s",
Expand Down Expand Up @@ -8078,7 +8090,19 @@ func GetFile(ctx context.Context, id string) (*File, error) {
if err := project.Dbclient.Get(ctx, key, curFile); err != nil {
return &File{}, err
}
}

if project.CacheDb {
fileData, err := json.Marshal(curFile)
if err != nil {
log.Printf("[WARNING] Failed marshalling in getfile: %s", err)
return curFile, nil
}

err = SetCache(ctx, cacheKey, fileData, 30)
if err != nil {
log.Printf("[WARNING] Failed setting cache for file key '%s': %s", cacheKey, err)
}
}

return curFile, nil
Expand Down Expand Up @@ -8132,6 +8156,12 @@ func SetFile(ctx context.Context, file File) error {
file.CreatedAt = timeNow
}

if !strings.HasPrefix(file.Id, "file_") {
return errors.New("Invalid file ID. Must start with file_")
}

cacheKey := fmt.Sprintf("%s_%s", nameKey, file.Id)

if project.DbType == "opensearch" {
data, err := json.Marshal(file)
if err != nil {
Expand All @@ -8151,6 +8181,23 @@ func SetFile(ctx context.Context, file File) error {
}
}

if project.CacheDb {
data, err := json.Marshal(file)
if err != nil {
log.Printf("[WARNING] Failed marshalling in setfile: %s", err)

} else {
err = SetCache(ctx, cacheKey, data, 30)
if err != nil {
log.Printf("[WARNING] Failed setting cache for set file '%s': %s", cacheKey, err)
}
}
}

DeleteCache(ctx, fmt.Sprintf("files_%s_%s", file.OrgId, file.Namespace))
DeleteCache(ctx, fmt.Sprintf("files_%s_", file.OrgId))


return nil
}

Expand Down
124 changes: 79 additions & 45 deletions files.go
Original file line number Diff line number Diff line change
Expand Up @@ -903,11 +903,25 @@ func HandleGetFileContent(resp http.ResponseWriter, request *http.Request) {
return
}

// Fixme: More auth: org and workflow!
// Automatically downloads and returns the file through resp
// GetFileContent() is used to return data, through resp if possible due to how we used to do it.

if len(file.OrgId) == 0 {
file.OrgId = user.ActiveOrg.Id
}

_, err = GetFileContent(ctx, file, resp)
if err != nil {
log.Printf("[ERROR] Failed getting file content for %s: %s", fileId, err)
}

//resp.WriteHeader(200)
//resp.Write(content)
}

func GetFileContent(ctx context.Context, file *File, resp http.ResponseWriter) ([]byte, error) {
downloadPath := file.DownloadPath
if project.Environment == "cloud" || file.StorageArea == "google_storage" {
//log.Printf("[AUDIT] %s (%s) downloaded file '%s' (%s) from google storage. Namespace: %s", user.Username, user.Id, file.Filename, file.Id, file.Namespace)

bucket := project.StorageClient.Bucket(orgFileBucket)
obj := bucket.Object(file.DownloadPath)
fileReader, err := obj.NewReader(ctx)
Expand All @@ -918,15 +932,21 @@ func HandleGetFileContent(resp http.ResponseWriter, request *http.Request) {
err = SetFile(ctx, *file)
if err != nil {
log.Printf("[ERROR] SetFile error while uploading")
resp.WriteHeader(500)
resp.Write([]byte(`{"success": false, "reason": "Failed setting file to deleted"}`))
return

if resp != nil {
resp.WriteHeader(500)
resp.Write([]byte(`{"success": false, "reason": "Failed setting file to deleted"}`))
}
return []byte{}, err
}

//File not found, send 404
resp.WriteHeader(404)
resp.Write([]byte(`{"success": false, "reason": "File doesn't exist in google cloud storage"}`))
return
if resp != nil {
resp.WriteHeader(404)
resp.Write([]byte(`{"success": false, "reason": "File doesn't exist in google cloud storage"}`))
}

return []byte{}, err
}

defer fileReader.Close()
Expand All @@ -949,12 +969,12 @@ func HandleGetFileContent(resp http.ResponseWriter, request *http.Request) {
}
}

passphrase := fmt.Sprintf("%s_%s", user.ActiveOrg.Id, file.Id)
passphrase := fmt.Sprintf("%s_%s", file.OrgId, file.Id)
data, err := HandleKeyDecryption(allText, passphrase)
if err != nil {
// Reference File Id only used as fallback
if len(file.ReferenceFileId) > 0 {
passphrase = fmt.Sprintf("%s_%s", user.ActiveOrg.Id, file.ReferenceFileId)
passphrase = fmt.Sprintf("%s_%s", file.OrgId, file.ReferenceFileId)
data, err = HandleKeyDecryption(allText, passphrase)
if err != nil {
log.Printf("[ERROR] Failed decrypting file (4): %s. Continuing anyway, but this WILL cause trouble for the user if the file is encrypted.", err)
Expand All @@ -974,22 +994,27 @@ func HandleGetFileContent(resp http.ResponseWriter, request *http.Request) {
FileSize := strconv.FormatInt(int64(len(allText)), 10) //Get file size as a string
//Send the headers
//log.Printf("Content Type: %#v", FileContentType)
resp.Header().Set("Content-Disposition", "attachment; filename="+file.Filename)
resp.Header().Set("Content-Type", FileContentType)
resp.Header().Set("Content-Length", FileSize)

reader := bytes.NewReader(allText)
io.Copy(resp, reader)
return
if resp != nil {
resp.Header().Set("Content-Disposition", "attachment; filename="+file.Filename)
resp.Header().Set("Content-Type", FileContentType)
resp.Header().Set("Content-Length", FileSize)
reader := bytes.NewReader(allText)
io.Copy(resp, reader)
}

return allText, nil

}

FileHeader := make([]byte, 512)
FileContentType := http.DetectContentType(FileHeader)
resp.Header().Set("Content-Disposition", "attachment; filename="+file.Filename)
resp.Header().Set("Content-Type", FileContentType)
if resp != nil {
FileHeader := make([]byte, 512)
FileContentType := http.DetectContentType(FileHeader)

io.Copy(resp, fileReader)
resp.Header().Set("Content-Disposition", "attachment; filename="+file.Filename)
resp.Header().Set("Content-Type", FileContentType)
io.Copy(resp, fileReader)
}

} else if file.StorageArea == "s3" {
log.Printf("[INFO] Trying to download file %s from s3", file.Id)
Expand All @@ -1002,18 +1027,24 @@ func HandleGetFileContent(resp http.ResponseWriter, request *http.Request) {
err = SetFile(ctx, *file)
if err != nil {
log.Printf("Failed setting file to uploading")
resp.WriteHeader(500)
resp.Write([]byte(`{"success": false, "reason": "Failed setting file to deleted"}`))
return
if resp != nil {
resp.WriteHeader(500)
resp.Write([]byte(`{"success": false, "reason": "Failed setting file to deleted"}`))
}

return []byte{}, err
}

//File not found, send 404
resp.WriteHeader(400)
resp.Write([]byte(`{"success": false, "reason": "File doesn't exist locally"}`))
return
if resp != nil {
resp.WriteHeader(400)
resp.Write([]byte(`{"success": false, "reason": "File doesn't exist locally"}`))
}

return []byte{}, err
}

log.Printf("[DEBUG] Should handle file decryption of %s.", fileId)
log.Printf("[DEBUG] Should handle file decryption of %s.", file.Id)
allText := []byte{}

buf := make([]byte, 1024)
Expand All @@ -1037,11 +1068,11 @@ func HandleGetFileContent(resp http.ResponseWriter, request *http.Request) {
Openfile.Close()

if file.Encrypted {
passphrase := fmt.Sprintf("%s_%s", user.ActiveOrg.Id, file.Id)
passphrase := fmt.Sprintf("%s_%s", file.OrgId, file.Id)
data, err := HandleKeyDecryption(allText, passphrase)
if err != nil {
if len(file.ReferenceFileId) > 0 {
passphrase = fmt.Sprintf("%s_%s", user.ActiveOrg.Id, file.ReferenceFileId)
passphrase = fmt.Sprintf("%s_%s", file.OrgId, file.ReferenceFileId)
data, err = HandleKeyDecryption(allText, passphrase)
if err != nil {
log.Printf("[ERROR] Failed decrypting file (5): %s", err)
Expand All @@ -1065,23 +1096,25 @@ func HandleGetFileContent(resp http.ResponseWriter, request *http.Request) {
FileSize := strconv.FormatInt(int64(len(allText)), 10) //Get file size as a string

//Send the headers
resp.Header().Set("Content-Disposition", "attachment; filename="+file.Filename)
resp.Header().Set("Content-Type", FileContentType)
resp.Header().Set("Content-Length", FileSize)

//resp.Write([]byte(allText))
if resp != nil {
resp.Header().Set("Content-Disposition", "attachment; filename="+file.Filename)
resp.Header().Set("Content-Type", FileContentType)
resp.Header().Set("Content-Length", FileSize)

//log.Printf("Md5: %#v", md5)
reader := bytes.NewReader(allText)
_, err = io.Copy(resp, reader)
if err != nil {
log.Printf("[ERROR] Failed copying info to request in download of %s: %s", file.Filename, err)
} else {
log.Printf("[INFO] Downloading %d bytes from file %s", len(allText), file.Filename)
//log.Printf("Md5: %#v", md5)
reader := bytes.NewReader(allText)
_, err = io.Copy(resp, reader)
if err != nil {
log.Printf("[ERROR] Failed copying info to request in download of %s: %s", file.Filename, err)
} else {
log.Printf("[INFO] Downloading %d bytes from file %s", len(allText), file.Filename)
}
}

return
return allText, nil
}

return nil, nil
}

func HandleEditFile(resp http.ResponseWriter, request *http.Request) {
Expand Down Expand Up @@ -1206,7 +1239,8 @@ func HandleUploadFile(resp http.ResponseWriter, request *http.Request) {
fileId = location[4]
}

if len(fileId) != 36 && !strings.HasPrefix(fileId, "file_") {
//if len(fileId) != 36 &&
if !strings.HasPrefix(fileId, "file_") || len(fileId) > 64 {
log.Printf("[WARNING] Bad format for fileId %s", fileId)
resp.WriteHeader(401)
resp.Write([]byte(`{"success": false, "reason": "Badly formatted fileId"}`))
Expand Down
Loading

0 comments on commit 221c15a

Please sign in to comment.