Exemplo n.º 1
0
// AssignValue assign form values to model.
func AssignValue(model interface{}, form interface{}) {
	modelIndirect := reflect.Indirect(reflect.ValueOf(model))
	formElem := reflect.ValueOf(form).Elem()
	typeOfTForm := formElem.Type()
	for i := 0; i < formElem.NumField(); i++ {
		modelField := modelIndirect.FieldByName(typeOfTForm.Field(i).Name)
		if modelField.IsValid() {
			formField := formElem.Field(i)
			modelField.Set(formField)
		} else {
			log.Warnf("modelField : %s - %s", typeOfTForm.Field(i).Name, modelField)
		}
	}
}
Exemplo n.º 2
0
// Concurrent.
func Concurrent(reader *multipart.Reader, result Result) (int, error) {
	c := make(chan Result)
	defer close(c)
	go func() {
		c <- result
	}()

	timeout := time.After(config.UploadTimeout)
	select {
	case res := <-c:
		log.Debugf("End of Upload : %v", res)
		workingNow := atomic.AddInt32(BusyWorker, -1)
		log.Debugf("All files are Done. Working concurrencyer count : %d", workingNow)
		if workingNow == 0 {
			return res.Code, res.Error
		}
	case <-timeout:
		err := errors.New("Request timed out.")
		log.Warnf(err.Error())
		return http.StatusBadRequest, err
	}
	return http.StatusBadRequest, errors.New("Invalid Request.")
}
Exemplo n.º 3
0
// articleUploader is a uploader that uploading files and sync articles.
func articleUploader() concurrency.ConcurrencyManager {
	return func(reader *multipart.Reader) concurrency.Result {
		atomic.AddInt32(concurrency.BusyWorker, 1)
		var articles []model.Article
		formDataBuffer := make([]byte, 100000)
		fileCount := 0
		sqlStrBuffer := new(bytes.Buffer)
		stringHelper.Concat(sqlStrBuffer, "INSERT INTO article(user_id, title, url, content, image_name, created_at) VALUES ")
		values := []interface{}{}
		var result concurrency.Result
		result.Code = http.StatusOK
		for {
			part, err := reader.NextPart()
			if err == io.EOF {
				break
			}
			if part.FileName() == "" {
				if part.FormName() != "" {
					log.Debug("formName : " + part.FormName())
					n, err := part.Read(formDataBuffer)
					if err != nil {
						log.Warnf("Multipart read failed. , (Error Detail : %s)", err.Error())
						result.Code = http.StatusBadRequest
						result.Error = err
						return result
					}
					log.Debugf("data : %s ", formDataBuffer)
					err = json.Unmarshal(formDataBuffer[:n], &articles)
					if err != nil {
						log.Warnf("Json unmarshal failed. , (Error Detail : %s)", err.Error())
						result.Code = http.StatusBadRequest
						result.Error = err
						return result
					}
					log.Debugf("err %s", err)
					log.Debugf("article : %v\n", articles)
					log.Debugf("article len : %d\n", len(articles))
				}
				continue
			}
			err = upload.UploadImageFile("", part)
			if err != nil {
				log.Error("Image uploading failed. : " + err.Error())
				result.Code = http.StatusBadRequest
				result.Error = err
				return result
			}
			if fileCount < len(articles) {
				stringHelper.Concat(sqlStrBuffer, "(?, ?, ?, ?, ?, ?),")
				values = append(values, user.Id, articles[fileCount].Title, articles[fileCount].Url, articles[fileCount].Content, articles[fileCount].ImageName, time.Now())
				// db.ORM.Create(&articles[fileCount])
				fileCount += 1
			}
			log.Debug("File uploaded.")
			log.Infof("File Count : %d\n", fileCount)
		}
		sqlStr := sqlStrBuffer.String()
		sqlStr = sqlStr[0 : len(sqlStr)-1]
		log.Debugf("sqlStr for Article : %s", sqlStr)
		db.ORM.Exec(sqlStr, values...)
		return result
	}
}