<commit_before>package api

import (
	"fmt"
	"octlink/rstore/modules/config"
	"octlink/rstore/modules/image"
	"octlink/rstore/utils"
	"octlink/rstore/utils/merrors"
	"octlink/rstore/utils/octlog"
	"octlink/rstore/utils/uuid"
)

// AddImage to add image by API
func AddImage(paras *Paras) *Response {

	resp := new(Response)

	id := paras.Get("id")
	if id != "" {
		im := image.GetImage(id)
		if im != nil {
			resp.Error = merrors.ErrSegmentAlreadyExist
			resp.ErrorLog = "User " + id + "Already Exist"
			return resp
		}
	} else {
		id = uuid.Generate().Simple()
	}

	im := new(image.Image)

	im.ID = id
	im.Arch = paras.Get("arch")
	im.Platform = paras.Get("platform")
	im.GuestOsType = paras.Get("guestOsType")
	im.Name = paras.Get("name")
	im.Desc = paras.Get("desc")
	im.MediaType = paras.Get("mediaType")
	im.Format = paras.Get("format")
	im.Account = paras.Get("accountId")
	im.CreateTime = utils.CurrentTimeStr()
	im.System = paras.GetBoolean("isSystem")
	im.URL = paras.Get("url1")
	im.Status = config.ImageStatusDownloading
	im.State = image.ImageStateEnabled
	im.LastSync = utils.CurrentTimeStr()

	im.Username = paras.Get("username")
	im.Password = <PASSWORD>as.Get("password")

	resp.Data, resp.Error = im.Add()

	return resp
}

// ShowImage by api
func ShowImage(paras *Paras) *Response {

	resp := new(Response)
	imageID := paras.InParas.Paras["id"].(string)

	temp := image.GetImage(imageID)
	if temp == nil {
		resp.Error = merrors.ErrSegmentNotExist
		resp.ErrorLog = fmt.Sprintf("user %s not found", imageID)
		return resp
	}

	resp.Data = temp

	return resp
}

// UpdateImage to update image by api
func UpdateImage(paras *Paras) *Response {
	resp := new(Response)

	id := paras.Get("id")
	im := image.GetImage(id)
	if im == nil {
		resp.Error = merrors.ErrUserNotExist
		resp.ErrorLog = "User " + id + "Not Exist"
		return resp
	}

	// Update Image here
	im.Arch = paras.Get("arch")
	im.Platform = paras.Get("platform")
	im.GuestOsType = paras.Get("guestOsType")
	im.Name = paras.Get("name")

	ret := im.Update()
	if ret != 0 {
		resp.Error = ret
		octlog.Error("update image of %s error\n", im.ID)
		return resp
	}

	return resp
}

// DeleteImageByAccount to delete image by account
func DeleteImageByAccount(paras *Paras) *Response {

	octlog.Debug("running in APIDeleteImage\n")

	resp := new(Response)

	images := image.GetAllImages(paras.InParas.Paras["accountId"].(string), "", "")
	for _, image := range images {
		err := image.Delete()
		if err != 0 {
			resp.Error = err
			octlog.Error("Errored when deleting image of %s\n", image.Name)
			return resp
		}
	}

	return resp
}

// DeleteImage to delete image
func DeleteImage(paras *Paras) *Response {

	octlog.Debug("running in APIDeleteImage\n")

	resp := new(Response)

	image := image.GetImage(paras.InParas.Paras["id"].(string))
	if image == nil {
		resp.Error = merrors.ErrSegmentNotExist
		return resp
	}

	resp.Error = image.Delete()

	return resp
}

// ShowAllImages to display all images by condition
func ShowAllImages(paras *Paras) *Response {
	resp := new(Response)

	octlog.Debug("running in APIShowAllImage\n")

	imageList := image.GetAllImages(paras.InParas.Paras["accountId"].(string),
		paras.InParas.Paras["mediaType"].(string), paras.InParas.Paras["keyword"].(string))

	result := make(map[string]interface{}, 3)
	result["total"] = 0
	result["count"] = len(imageList)
	result["data"] = imageList

	resp.Data = result

	return resp
}

// ShowAccountList of this rstore server
func ShowAccountList(paras *Paras) *Response {

	octlog.Debug("running in APIShowAllImage\n")

	resp := new(Response)

	resp.Data = image.GetAccountList()

	return resp
}
<commit_msg>for api of image list all<commit_after>package api

import (
	"fmt"
	"octlink/rstore/modules/config"
	"octlink/rstore/modules/image"
	"octlink/rstore/utils"
	"octlink/rstore/utils/merrors"
	"octlink/rstore/utils/octlog"
	"octlink/rstore/utils/uuid"
)

// AddImage to add image by API
func AddImage(paras *Paras) *Response {

	resp := new(Response)

	id := paras.Get("id")
	if id != "" {
		im := image.GetImage(id)
		if im != nil {
			resp.Error = merrors.ErrSegmentAlreadyExist
			resp.ErrorLog = "User " + id + "Already Exist"
			return resp
		}
	} else {
		id = uuid.Generate().Simple()
	}

	im := new(image.Image)

	im.ID = id
	im.Arch = paras.Get("arch")
	im.Platform = paras.Get("platform")
	im.GuestOsType = paras.Get("guestOsType")
	im.Name = paras.Get("name")
	im.Desc = paras.Get("desc")
	im.MediaType = paras.Get("mediaType")
	im.Format = paras.Get("format")
	im.Account = paras.Get("accountId")
	im.CreateTime = utils.CurrentTimeStr()
	im.System = paras.GetBoolean("isSystem")
	im.URL = paras.Get("url1")
	im.Status = config.ImageStatusDownloading
	im.State = image.ImageStateEnabled
	im.LastSync = utils.CurrentTimeStr()

	im.Username = paras.Get("username")
	im.Password = <PASSWORD>("password")

	resp.Data, resp.Error = im.Add()

	return resp
}

// ShowImage by api
func ShowImage(paras *Paras) *Response {

	resp := new(Response)
	imageID := paras.InParas.Paras["id"].(string)

	temp := image.GetImage(imageID)
	if temp == nil {
		resp.Error = merrors.ErrSegmentNotExist
		resp.ErrorLog = fmt.Sprintf("user %s not found", imageID)
		return resp
	}

	resp.Data = temp

	return resp
}

// UpdateImage to update image by api
func UpdateImage(paras *Paras) *Response {
	resp := new(Response)

	id := paras.Get("id")
	im := image.GetImage(id)
	if im == nil {
		resp.Error = merrors.ErrUserNotExist
		resp.ErrorLog = "User " + id + "Not Exist"
		return resp
	}

	// Update Image here
	im.Arch = paras.Get("arch")
	im.Platform = paras.Get("platform")
	im.GuestOsType = paras.Get("guestOsType")
	im.Name = paras.Get("name")

	ret := im.Update()
	if ret != 0 {
		resp.Error = ret
		octlog.Error("update image of %s error\n", im.ID)
		return resp
	}

	return resp
}

// DeleteImageByAccount to delete image by account
func DeleteImageByAccount(paras *Paras) *Response {

	octlog.Debug("running in APIDeleteImage\n")

	resp := new(Response)

	images := image.GetAllImages(paras.InParas.Paras["accountId"].(string), "", "")
	for _, image := range images {
		err := image.Delete()
		if err != 0 {
			resp.Error = err
			octlog.Error("Errored when deleting image of %s\n", image.Name)
			return resp
		}
	}

	return resp
}

// DeleteImage to delete image
func DeleteImage(paras *Paras) *Response {

	octlog.Debug("running in APIDeleteImage\n")

	resp := new(Response)

	image := image.GetImage(paras.InParas.Paras["id"].(string))
	if image == nil {
		resp.Error = merrors.ErrSegmentNotExist
		return resp
	}

	resp.Error = image.Delete()

	return resp
}

// ShowAllImages to display all images by condition
func ShowAllImages(paras *Paras) *Response {
	resp := new(Response)

	octlog.Debug("running in APIShowAllImage\n")

	imageList := image.GetAllImages(paras.Get("accountId"),
		paras.Get("mediaType"), paras.Get("keyword"))

	resp.Data = map[string]interface{}{
		"total": len(imageList),
		"count": len(imageList),
		"data":  imageList,
	}

	return resp
}

// ShowAccountList of this rstore server
func ShowAccountList(paras *Paras) *Response {

	octlog.Debug("running in APIShowAllImage\n")

	resp := new(Response)

	resp.Data = image.GetAccountList()

	return resp
}

{'fraction_non_alphanumeric': 0.1499861763892729, 'fraction_numerical': 0.001105888858169754, 'mean_word_length': 4.017337031900139, 'pattern_counts': {'":': 3, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2709385', 'n_tokens_mistral': 2728, 'n_tokens_neox': 2586, 'n_words': 574}