github.com/msinev/pmc@v0.0.0-20220512222710-eb40bf080315/pkg/uploader/result.go (about) 1 package main 2 3 import ( 4 "io" 5 "log" 6 "net/http" 7 "os" 8 "path" 9 "sync" 10 "time" 11 ) 12 13 func readLoopResult(part io.Reader, fname string, code string, filetype string) error { 14 dst, err := os.Create(fname) 15 16 if err != nil { 17 log.Printf("Created path error %s %v", fname, err) 18 return err 19 } 20 21 var read int64 22 lastBuff := false 23 for !lastBuff { 24 buffer := make([]byte, 100000) 25 cBytes, err := part.Read(buffer) 26 if err != io.EOF && err != nil { 27 return err 28 } 29 if err == nil && cBytes <= 0 { 30 return io.ErrNoProgress 31 } 32 33 read = read + int64(cBytes) 34 35 //fmt.Printf("\r read: %v length : %v \n", read, length) 36 lastBuff = (err == io.EOF) 37 38 count, err := dst.Write(buffer[0:cBytes]) 39 if err != nil { 40 return err 41 } 42 43 if count != cBytes { 44 log.Printf("Unable to write %d, only %d written", cBytes, count) 45 return io.ErrShortWrite 46 } 47 ts := time.Now() 48 SyncOperations <- func() { 49 50 task, ok := IDs[code] 51 if !ok || task == nil { 52 return 53 } 54 task.TS = ts 55 56 if task.ResultUpload == nil { 57 return 58 } 59 up := task.ResultUpload[filetype] 60 61 if up.Completed != nil { 62 return 63 } 64 65 up.Uploaded = read 66 67 if lastBuff { 68 up.Completed = &ts 69 } 70 } 71 72 } 73 return io.EOF 74 } 75 76 var FileTypes map[string]int 77 78 func checkTypeOk(ftype string) bool { 79 _, ok := FileTypes[ftype] 80 return ok 81 } 82 83 func uploadResultFile(w http.ResponseWriter, r *http.Request) { 84 if r.Method == "OPTIONS" { 85 ReplyJSONOptions(w, r) 86 return 87 } 88 89 keys, ok := r.URL.Query()["hcode"] 90 code := "" 91 if !ok || len(keys[0]) < 1 { 92 log.Print("No hcode in URL") 93 } else { 94 code = keys[0] 95 } 96 log.Printf("Uploading file with HCODE %s", code) 97 98 filetypes, ok := r.URL.Query()["type"] 99 if len(filetypes) != 1 { 100 if len(filetypes) == 0 { 101 ReplyJSONString("\"File type missing\"", w, r) 102 return 103 } 104 ReplyJSONString("\"File type count error\"", w, r) 105 return 106 } 107 filetype := filetypes[0] 108 if checkTypeOk(filetype) { 109 ReplyJSONString("\"File type invalid\"", w, r) 110 return 111 } 112 113 log.Printf("Uploading file with HCODE %s", code) 114 115 initLoad := false 116 aper := &initLoad 117 118 mr, err := r.MultipartReader() 119 if err != nil { 120 ReplyJSONString("\""+err.Error()+"\"", w, r) 121 return 122 } 123 124 length := r.ContentLength 125 var Mutex sync.Mutex 126 var Signal *sync.Cond 127 Signal = sync.NewCond(&Mutex) 128 Mutex.Lock() 129 130 ts := time.Now() 131 SyncOperations <- func() { 132 Mutex.Lock() 133 defer Mutex.Unlock() 134 defer Signal.Signal() 135 task, ok := IDs[code] 136 if !ok || task == nil { 137 return 138 } 139 task.TS = ts 140 if task.ResultUpload[filetype] != nil { 141 return 142 } 143 up := &UploadProgress{ 144 Size: length, 145 Uploaded: 0, 146 Created: ts, 147 TS: ts, 148 Completed: nil, 149 } 150 151 task.ResultUpload[filetype] = up 152 153 *aper = true 154 } 155 156 Signal.Wait() 157 Mutex.Unlock() 158 159 if !initLoad { 160 ReplyJSONString("\"Already uploading\"", w, r) 161 return 162 } 163 newPath := path.Join(BasePath, code) 164 err = os.Mkdir(newPath, 0755) 165 if err != nil { 166 log.Printf("Failed to created path %s %v", newPath, err) 167 ReplyJSONString("\"Impossible to create target path\"", w, r) 168 return 169 } 170 log.Printf("Created path %s", newPath) 171 //ticker := time.Tick(time.Millisecond) // <-- use this in production 172 //ticker := time.Tick(time.Second) // this is for demo purpose with longer delay 173 174 filePath := path.Join(newPath, "input.json") 175 for { 176 177 part, err := mr.NextPart() 178 name := part.FormName() 179 if name == "file" { 180 err = readLoopResult(part, filePath, code, filetype) 181 if err != io.EOF && err != nil { 182 log.Printf("Error writing file %s %v", filePath, err) 183 ReplyJSONString("\"Error writing file\"", w, r) 184 break 185 } 186 } else { 187 log.Printf("Ignoring field %s", name) 188 buff, count, err := readLoopStub(part) 189 log.Printf("Bytes read %d, %s ", count, string(buff[0:Min(int(count), len(buff))])) 190 if err != io.EOF { 191 panic(err) 192 } 193 err = nil 194 } 195 //fn := part.FormName() 196 197 if err == io.EOF { 198 log.Printf("\nDone uploading %s", code) 199 TaskQueueInsert(code) 200 ReplyJSON([]byte("\"OK\""), w, r, false) 201 break 202 } 203 204 } 205 }