diff --git a/tools/tools.go b/tools/tools.go index 9628027..47e4cfe 100644 --- a/tools/tools.go +++ b/tools/tools.go @@ -137,8 +137,8 @@ func handleAnswer(file *excelize.File, sheet, cell, s3Prefix string, answer mode noAccept := make(map[string]struct{}) todoMap := make(map[string]string) - if tipe != "Text" && question.Type == model.TypeImages || question.Type == model.TypeVarImages { - handleImage(file, sheet, cell, answer.Content, count, row, noAccept, todoMap) + if tipe != "Text" && (question.Type == model.TypeImages || question.Type == model.TypeVarImages) { + handleImage(file, sheet, cell, answer.Content, count, row, noAccept, todoMap, question.Title) } else if question.Type == model.TypeFile { handleFile(file, sheet, cell, answer.Content, s3Prefix, noAccept) } else { @@ -154,32 +154,129 @@ func handleAnswer(file *excelize.File, sheet, cell, s3Prefix string, answer mode } } -func handleImage(file *excelize.File, sheet, cell, content string, count, row int, noAccept map[string]struct{}, todoMap map[string]string) { - var res model.ImageContent - err := json.Unmarshal([]byte(content), &res) - if err != nil { - res.Image = content - } - urle := ExtractImageURL(res.Image) - urlData := strings.Split(urle, " ") - if len(urlData) == 1 { - u, err := url.Parse(urle) - if err == nil && u.Scheme != "" && u.Host != "" { - picture, err := downloadImage(urle) +func handleImage(file *excelize.File, sheet, cell, content string, count, row int, noAccept map[string]struct{}, todoMap map[string]string, questionTitle string) { + multiImgArr := strings.Split(content, "`,`") + if len(multiImgArr) > 1 { + var descriptions []string + mediaSheet := "Media" + flag, err := file.GetSheetIndex(mediaSheet) + if err != nil { + fmt.Println(err.Error()) + } + if flag == -1 { + _, _ = file.NewSheet(mediaSheet) + err = file.SetCellValue(mediaSheet, "A1", "Вопрос") if err != nil { fmt.Println(err.Error()) } - file.SetColWidth(sheet, ToAlphaString(count), ToAlphaString(count), 50) - file.SetRowHeight(sheet, row, 150) - if err := file.AddPictureFromBytes(sheet, cell, picture); err != nil { + } + + mediaRow := row + for i, imgContent := range multiImgArr { + if i == 0 && len(imgContent) > 1 && imgContent[0] == '`' { + imgContent = imgContent[1:] + } + + if i == len(multiImgArr)-1 && len(imgContent) > 1 && imgContent[len(imgContent)-1] == '`' { + imgContent = imgContent[:len(imgContent)-1] + } + + var res model.ImageContent + err := json.Unmarshal([]byte(imgContent), &res) + if err != nil { + res.Image = imgContent + } + + // чек на пустой дескрипшен, есмли пустой то отмечаем как вариант ответа номер по i + if res.Description != "" { + descriptions = append(descriptions, res.Description) + } else { + descriptions = append(descriptions, fmt.Sprintf("Вариант ответа №%d", i+1)) + } + + urle := ExtractImageURL(res.Image) + urlData := strings.Split(urle, " ") + if len(urlData) == 1 { + u, err := url.Parse(urle) + if err == nil && u.Scheme != "" && u.Host != "" { + picture, err := downloadImage(urle) + if err != nil { + fmt.Println(err.Error()) + continue + } + err = file.SetCellValue(mediaSheet, "A"+strconv.Itoa(mediaRow), questionTitle) + if err != nil { + fmt.Println(err.Error()) + } + + col := ToAlphaString(i + 2) + err = file.SetColWidth(mediaSheet, col, col, 50) + if err != nil { + fmt.Println(err.Error()) + } + err = file.SetRowHeight(mediaSheet, mediaRow, 150) + if err != nil { + fmt.Println(err.Error()) + } + if err := file.AddPictureFromBytes(mediaSheet, col+strconv.Itoa(mediaRow), picture); err != nil { + fmt.Println(err.Error()) + } + noAccept[content] = struct{}{} + } else { + todoMap[content] = cell + } + } else { + todoMap[imgContent] = cell + } + + descriptionsStr := strings.Join(descriptions, "`,`") + linkText := fmt.Sprintf("%s, Приложение: %s!A%d", descriptionsStr, mediaSheet, mediaRow) + + if err := file.SetCellValue(sheet, cell, linkText); err != nil { fmt.Println(err.Error()) } - noAccept[content] = struct{}{} + if err := file.SetCellHyperLink(sheet, cell, fmt.Sprintf("%s!A%d", mediaSheet, mediaRow), "Location", excelize.HyperlinkOpts{ + Display: &linkText, + }); err != nil { + fmt.Println(err.Error()) + } + } + } else { + if len(content) > 1 && content[0] == '`' && content[len(content)-1] == '`' { + content = content[1 : len(content)-1] + } + var res model.ImageContent + err := json.Unmarshal([]byte(content), &res) + if err != nil { + res.Image = content + } + urle := ExtractImageURL(res.Image) + urlData := strings.Split(urle, " ") + if len(urlData) == 1 { + u, err := url.Parse(urle) + if err == nil && u.Scheme != "" && u.Host != "" { + picture, err := downloadImage(urle) + if err != nil { + fmt.Println(err.Error()) + } + err = file.SetColWidth(sheet, ToAlphaString(count), ToAlphaString(count), 50) + if err != nil { + fmt.Println(err.Error()) + } + err = file.SetRowHeight(sheet, row, 150) + if err != nil { + fmt.Println(err.Error()) + } + if err := file.AddPictureFromBytes(sheet, cell, picture); err != nil { + fmt.Println(err.Error()) + } + noAccept[content] = struct{}{} + } else { + todoMap[content] = cell + } } else { todoMap[content] = cell } - } else { - todoMap[content] = cell } }