コード例 #1
0
ファイル: file.go プロジェクト: tka/goyangi
// CreateFiles creates files.
func CreateFiles(c *gin.Context) (int, error) {
	var forms FilesForm
	start := time.Now()
	c.BindWith(&forms, binding.JSON)
	log.Debugf("CreateFiles c form : %v", forms)

	user, _ := userService.CurrentUser(c)
	sqlStrBuffer := new(bytes.Buffer)
	stringHelper.Concat(sqlStrBuffer, "INSERT INTO file(user_id, name, size, created_at) VALUES ")
	values := []interface{}{}
	for _, file := range forms.Files {
		stringHelper.Concat(sqlStrBuffer, "(?, ?, ?, ?),")
		values = append(values, user.Id, file.Name, file.Size, time.Now())

	}
	// sqlStrBuffer.Truncate(sqlStrBuffer.Len() - 1) is slower than slice.
	if len(values) > 0 {
		sqlStr := sqlStrBuffer.String()
		sqlStr = sqlStr[0 : len(sqlStr)-1]
		log.Debugf("sqlStr for File : %s", sqlStr)
		db.ORM.Exec(sqlStr, values...)
		elapsed := time.Since(start)
		log.Debugf("CreateFiles elapsed : %s", elapsed)
	}

	return http.StatusCreated, nil
}
コード例 #2
0
ファイル: location.go プロジェクト: wangmingjob/goyangi
// RetrieveLocations retrieves locations.
func RetrieveLocations(c *gin.Context) ([]model.Location, bool, int, bool, bool, int, error) {
	var locations []model.Location
	var locationCount, locationPerPage int
	filterQuery := c.Request.URL.Query().Get("filter")
	locationPerPage = config.LocationPerPage
	filter := &LocationFilter{}
	whereBuffer := new(bytes.Buffer)
	whereValues := []interface{}{}
	if len(filterQuery) > 0 {
		log.Debugf("retrieve Locations filter : %s\n", filterQuery)
		json.Unmarshal([]byte(filterQuery), &filter)
		if filter.UserId > 0 {
			stringHelper.Concat(whereBuffer, "user_id = ?")
			whereValues = append(whereValues, filter.UserId)
			log.Debugf("userId : %d\n", filter.UserId)
		}
		if filter.LocationPerPage > 0 {
			locationPerPage = filter.LocationPerPage
			log.Debugf("locationPerPage : %d\n", filter.LocationPerPage)
		}
	} else {
		log.Debug("no filters found.\n")
	}
	log.Debugf("filterQuery %v.\n", filterQuery)
	log.Debugf("filter %v.\n", filter)
	whereStr := whereBuffer.String()
	db.ORM.Model(model.Location{}).Where(whereStr, whereValues...).Count(&locationCount)
	offset, currentPage, hasPrev, hasNext := pagination.Paginate(filter.CurrentPage, locationPerPage, locationCount)
	db.ORM.Limit(locationPerPage).Offset(offset).Order(config.LocationOrder).Where(whereStr, whereValues...).Find(&locations)

	return locations, canUserWrite(c), currentPage, hasPrev, hasNext, http.StatusOK, nil
}
コード例 #3
0
ファイル: upload.go プロジェクト: tka/goyangi
// articleUploader is a uploader that uploading files and sync articles.
func articleUploader() Uploader {
	return func(reader *multipart.Reader) UploadStatus {
		atomic.AddInt32(workingUploader, 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{}{}

		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)
					log.Debugf("n, err %d %s", n, err)
					log.Debugf("data : %s ", formDataBuffer)
					err = json.Unmarshal(formDataBuffer[:n], &articles)
					log.Debugf("err %s", err)
					log.Debugf("article : %v\n", articles)
					log.Debugf("article len : %d\n", len(articles))
				}
				continue
			}
			UploadImageFile(part)
			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 UploadStatus(true)
	}
}
コード例 #4
0
ファイル: article.go プロジェクト: tka/goyangi
// RetrieveArticles retrieves articles.
func RetrieveArticles(c *gin.Context) ([]model.Article, bool, int, int, bool, bool, int, error) {
	var articles []model.Article
	var category int
	var articleCount, articlePerPage int
	filterQuery := c.Request.URL.Query().Get("filter")
	articlePerPage = config.ArticlePerPage
	filter := &ArticleFilter{}
	whereBuffer := new(bytes.Buffer)
	whereValues := []interface{}{}
	if len(filterQuery) > 0 {
		log.Debugf("retrieve Articles filter : %s\n", filterQuery)
		json.Unmarshal([]byte(filterQuery), &filter)
		if filter.UserId > 0 {
			stringHelper.Concat(whereBuffer, "user_id = ?")
			whereValues = append(whereValues, filter.UserId)
			log.Debugf("userId : %d\n", filter.UserId)
		}
		if len(filter.Categories) > 0 {
			if len(whereValues) == 1 {
				stringHelper.Concat(whereBuffer, " and ")
			}
			stringHelper.Concat(whereBuffer, "category_id = ?")
			whereValues = append(whereValues, filter.Categories[0])
			log.Debugf("categories : %d\n", filter.Categories[0])
			category = filter.Categories[0]
		}
		if filter.ArticlePerPage > 0 {
			articlePerPage = filter.ArticlePerPage
			log.Debugf("articlePerPage : %d\n", filter.ArticlePerPage)
		}
	} else {
		log.Debug("no filters found.\n")
	}
	log.Debugf("filterQuery %v.\n", filterQuery)
	log.Debugf("filter %v.\n", filter)
	whereStr := whereBuffer.String()
	log.Debugf("whereStr %s.\n", whereStr)
	log.Debugf("whereValues %v.\n", whereValues)
	db.ORM.Model(model.Article{}).Where(whereStr, whereValues...).Count(&articleCount)
	offset, currentPage, hasPrev, hasNext := pagination.Paginate(filter.CurrentPage, articlePerPage, articleCount)
	log.Debugf("currentPage, perPage, total : %d, %d, %d", filter.CurrentPage, articlePerPage, articleCount)
	log.Debugf("offset, currentPage, hasPrev, hasNext : %d, %d, %t, %t", offset, currentPage, hasPrev, hasNext)
	db.ORM.Limit(articlePerPage).Offset(offset).Order(config.ArticleOrder).Where(whereStr, whereValues...).Find(&articles)
	return articles, canUserWrite(c, category), category, currentPage, hasPrev, hasNext, http.StatusOK, nil
}
コード例 #5
0
ファイル: article.go プロジェクト: tka/goyangi
// CreateArticles creates articles.
func CreateArticles(c *gin.Context) (int, error) {
	var forms ArticlesForm
	c.BindWith(&forms, binding.JSON)
	log.Debugf("CreateFiles c form : %v", forms)

	user, _ := userService.CurrentUser(c)
	sqlStrBuffer := new(bytes.Buffer)
	stringHelper.Concat(sqlStrBuffer, "INSERT INTO article(user_id, title, url, content, image_name, category_id, created_at) VALUES ")
	values := []interface{}{}
	for _, article := range forms.Articles {
		stringHelper.Concat(sqlStrBuffer, "(?, ?, ?, ?, ?, ?, ?),")
		values = append(values, user.Id, article.Title, article.Url, article.Content, article.ImageName, 0, time.Now())
	}
	// sqlStrBuffer.Truncate(sqlStrBuffer.Len() - 1) is slower than slice.
	if len(values) > 0 {
		sqlStr := sqlStrBuffer.String()
		sqlStr = sqlStr[0 : len(sqlStr)-1]
		log.Debugf("sqlStr for File : %s", sqlStr)
		db.ORM.Exec(sqlStr, values...)
	}

	return http.StatusCreated, nil
}
コード例 #6
0
ファイル: share.go プロジェクト: wangmingjob/goyangi
// 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
	}
}