#141: Added file meta to file uploads

This commit is contained in:
frikky
2020-11-23 15:50:31 +01:00
parent 1e58361150
commit acc6378dc2
5 changed files with 202 additions and 14 deletions
+29 -10
View File
@@ -830,21 +830,40 @@ class AppBase:
def get_files(full_execution, value):
org_id = full_execution["workflow"]["execution_org"]["id"]
print("SHOULD GET FILES BASED ON ORG %s, workflow %s and value(s) %s" % (org_id, full_execution["workflow"]["id"], value))
get_path = "/api/v1/files/%s/content?execution_id=%s" % (value, full_execution["execution_id"])
print("PATH: %s" % get_path)
get_path = "/api/v1/files/%s?execution_id=%s" % (value, full_execution["execution_id"])
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer %s" % self.authorization
}
ret = requests.get("%s%s" % (self.url, get_path), headers=headers)
if ret.status_code == 200:
return ret.text
print("ERROR GETTING FILE: ")
print("FILE RET CONTENT: %s" % ret.text)
print("FILE RET CODE FILE: %d" % ret.status_code)
return "Error getting file(s). Status code %d" % ret.status_code
ret1 = requests.get("%s%s" % (self.url, get_path), headers=headers)
print("RET1: %s" % ret1.text)
if ret1.status_code != 200:
return {
"filename": "",
"data": "",
"success": False,
}
content_path = "/api/v1/files/%s/content?execution_id=%s" % (value, full_execution["execution_id"])
ret2 = requests.get("%s%s" % (self.url, content_path), headers=headers)
print("Ret2: %s" % ret2.text)
if ret2.status_code == 200:
tmpdata = ret1.json()
returndata = {
"success": True,
"filename": tmpdata["filename"],
"data": ret2.text,
}
return returndata
return {
"success": False,
"filename": "",
"data": "",
}
# Sets files in the backend
def set_files(full_execution, infiles):
@@ -1094,7 +1113,7 @@ class AppBase:
# This code handles files.
try:
if parameter["schema"]["type"] == "file":
if parameter["schema"]["type"] == "file" and len(value) > 0:
print("SHOULD HANDLE FILE. Get based on value %s" % parameter["value"])
file_value = get_files(fullexecution, value)
print("FILE VALUE: %s" % file_value)
+85
View File
@@ -92,6 +92,91 @@ func fileExists(filename string) bool {
return !info.IsDir()
}
func handleGetFileMeta(resp http.ResponseWriter, request *http.Request) {
cors := handleCors(resp, request)
if cors {
return
}
var fileId string
location := strings.Split(request.URL.String(), "/")
if location[1] == "api" {
if len(location) <= 4 {
log.Printf("[INFO] Path too short: %d", len(location))
resp.WriteHeader(401)
resp.Write([]byte(`{"success": false}`))
return
}
fileId = location[4]
}
if strings.Contains(fileId, "?") {
fileId = strings.Split(fileId, "?")[0]
}
log.Printf("\n\n[INFO] User is trying to delete file %s\n\n", fileId)
// 1. Check user directly
// 2. Check workflow execution authorization
user, err := handleApiAuthentication(resp, request)
if err != nil {
log.Printf("[INFO] INITIAL Api authentication failed in file deletion: %s", err)
orgId, err := fileAuthentication(request)
if err != nil {
log.Printf("[ERROR] Bad file authentication in get: %s", err)
resp.WriteHeader(401)
resp.Write([]byte(`{"success": false}`))
return
}
user.ActiveOrg.Id = orgId
user.Username = "Execution File API"
}
// 1. Verify if the user has access to the file: org_id and workflow
log.Printf("[INFO] Should DELETE file %s if user has access", fileId)
ctx := context.Background()
file, err := getFile(ctx, fileId)
if err != nil {
log.Printf("[INFO] File %s not found: %s", fileId, err)
resp.WriteHeader(401)
resp.Write([]byte(`{"success": false}`))
return
}
found := false
if file.OrgId == user.ActiveOrg.Id {
found = true
} else {
for _, item := range user.Orgs {
if item == file.OrgId {
found = true
break
}
}
}
if !found {
log.Printf("[INFO] User %s doesn't have access to %s", user.Username, fileId)
resp.WriteHeader(401)
resp.Write([]byte(`{"success": false}`))
return
}
newBody, err := json.Marshal(file)
if err != nil {
resp.WriteHeader(500)
resp.Write([]byte(`{"success": false, "reason": "Failed to marshal filedata"}`))
return
}
log.Printf("[INFO] Successfully deleted file %s", fileId)
resp.WriteHeader(200)
resp.Write([]byte(newBody))
}
func handleDeleteFile(resp http.ResponseWriter, request *http.Request) {
cors := handleCors(resp, request)
if cors {
+1 -1
View File
@@ -8048,7 +8048,7 @@ func initHandlers() {
r.HandleFunc("/api/v1/files/{fileId}", handleDeleteFile).Methods("DELETE", "OPTIONS")
r.HandleFunc("/api/v1/files/create", handleCreateFile).Methods("POST", "OPTIONS")
r.HandleFunc("/api/v1/files/{fileId}/upload", handleUploadFile).Methods("POST", "OPTIONS")
//r.HandleFunc("/api/v1/files/{fileId}", handleGetFile).Methods("GET", "OPTIONS")
r.HandleFunc("/api/v1/files/{fileId}", handleGetFileMeta).Methods("GET", "OPTIONS")
http.Handle("/", r)
}