github.com/msinev/pmc@v0.0.0-20220512222710-eb40bf080315/pkg/uploader/xform.go (about)

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