-
Notifications
You must be signed in to change notification settings - Fork 32
Dev: Webpを画像形式に追加 #2981
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Dev: Webpを画像形式に追加 #2981
Changes from 1 commit
85c0799
d6a7ccd
ec8b2e1
2a62d20
4bc806c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,9 +3,10 @@ | |
|
|
||
| import ( | ||
| "bytes" | ||
| "image/png" | ||
| "io" | ||
|
|
||
| "github.com/sapphi-red/midec" | ||
| _ "github.com/sapphi-red/midec/webp" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. import が sort されていなさそう(どちらかというと linter が落ちないのが悪い気がするが)
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sortしました |
||
| "github.com/gofrs/uuid" | ||
| "github.com/labstack/echo/v4" | ||
|
|
||
|
|
@@ -81,6 +82,34 @@ | |
| args.MimeType = consts.MimeImagePNG | ||
| args.Thumbnail = img // サムネイル画像より小さいという前提 | ||
|
|
||
| case consts.MimeImageWebP: | ||
|
uni-kakurenbo marked this conversation as resolved.
|
||
| isAnimated, _ := midec.IsAnimated(src) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. MUST: err ハンドリングをしてほしい isAnimated, err := mide.IsAnimated(src)
if err != nil {
return uuid.Nil, herror.InternalServerError(err)
}
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. エラーハンドリングを追加しました
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. comment: つまり、 seekErr チェック → isAnimated → err チェック → if isAnimated の順 |
||
| if _, seekErr := src.Seek(0, io.SeekStart); seekErr != nil { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. reeにしました |
||
| return uuid.Nil, herror.InternalServerError(seekErr) | ||
| } | ||
| if isAnimated { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| return uuid.Nil, herror.BadRequest("animated WebP is not supported") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. want: アイコンもスタンプも animation 対応できるので、 TODO コメントを付けるかアニメーションも対応できるかどっちかはしてほしいかも!例えば とか
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 依存とかクロップとか考えて色々大変なので一旦TODOで書いておきました
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. あとで issue 立ててもらえるとうれしい |
||
| } | ||
| img, err := p.Fit(src, maxImageSize, maxImageSize) | ||
| if err != nil { | ||
| switch err { | ||
| case imaging.ErrInvalidImageSrc: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. errors.Is を使ってほしいかも?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. errors.Isを使うように変えました |
||
| return uuid.Nil, herror.BadRequest(badImage) | ||
| case imaging.ErrPixelLimitExceeded: | ||
| return uuid.Nil, herror.BadRequest(tooLargeImage) | ||
| default: | ||
| return uuid.Nil, herror.InternalServerError(err) | ||
| } | ||
| } | ||
| b := bytes.Buffer{} | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nits: (好みの問題です) 個人的には、
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. var bの書き方に変えました |
||
| if err := png.Encode(&b, img); err != nil { | ||
| return uuid.Nil, herror.InternalServerError(err) | ||
| } | ||
| args.Src = bytes.NewReader(b.Bytes()) | ||
| args.FileSize = int64(b.Len()) | ||
| args.MimeType = consts.MimeImagePNG | ||
| args.Thumbnail = img | ||
|
Comment on lines
+99
to
+117
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🩺 Stability & Availability | 🟠 Major | 🏗️ Heavy lift 🧩 Analysis chain🏁 Script executed: #!/usr/bin/env bash
set -euo pipefail
# Inspect all wrappers, helper calls, and imaging processor contracts.
rg -n --glob '*.go' \
'SaveUploadIconImage|SaveUploadStampImage|saveUploadImage|type Processor interface|func .*Fit\(|\.Fit\(' \
router serviceRepository: traPtitech/traQ Length of output: 2043 🏁 Script executed: #!/usr/bin/env bash
set -euo pipefail
printf '%s\n' '--- candidate files ---'
git ls-files | rg '(^|/)(process_image\.go|.*_test\.go|go\.mod)$' | head -200
printf '%s\n' '--- target file ---'
if [ -f router/utils/process_image.go ]; then
cat -n router/utils/process_image.go
else
printf '%s\n' 'router/utils/process_image.go not present'
fi
printf '%s\n' '--- related call sites and declarations ---'
rg -n --glob '*.go' \
'SaveUploadIconImage|SaveUploadStampImage|saveUploadImage|type Processor interface|func .*Fit\(|\.Fit\(|midec\.IsAnimated|nativewebp\.Encode|\.Save\(' \
. | head -400
printf '%s\n' '--- module declarations ---'
if [ -f go.mod ]; then
rg -n 'imaging|midec|webp' go.mod go.sum
fiRepository: traPtitech/traQ Length of output: 2014 🌐 Web query:
💡 Result: In the traQ backend repository (traPtitech/traQ), the file router/utils/process_image.go contains image processing logic, including the function saveUploadImage [1][2]. This function is used by higher-level handlers such as SaveUploadIconImage (for user icons) and others (e.g., for stamps) to process and store uploaded image files [1]. Recent developments in the repository indicate that saveUploadImage handles various image formats and includes validation and conversion logic [1]. For instance, a pull request ( Citations: 🌐 Web query:
💡 Result: In the traQ (traP Internal Messenger Application) project, "imaging" refers to the system's image processing functionality, which utilizes the third-party Go package github.com/disintegration/imaging [1][2]. The term "Fit" is a specific function provided by the Citations:
🌐 Web query:
💡 Result: The Go library sapphi-red/midec is a multi-image detector used to determine if an image file (such as GIF, APNG, WebP, or HEIF/AVIF) is animated [1][2]. Regarding the specific terms in your query: IsAnimated: This is the primary function provided by the midec package [1][2]. It accepts an io.Reader and returns a boolean value indicating whether the input image is animated [1]. ErrFormat: This is not a component of the midec library itself, but rather a standard error variable defined in the Go standard library's image package (image.ErrFormat) [3][4]. It is commonly used in Go to indicate that an image format is unknown or not registered [3][5]. Because midec is designed to be similar to the standard image package, it may interact with or follow patterns similar to those where image.ErrFormat is used, but it is not part of the midec API [1][2]. In summary, midec provides the IsAnimated functionality to check for multi-image/animated content, while ErrFormat is a standard Go library constant for handling unknown image formats [1][3]. Citations:
🌐 Web query:
💡 Result: No matching result was found for that exact query. The 🌐 Web query:
💡 Result:
It:
Citations: 🌐 Web query:
💡 Result: The terms in your query refer to two distinct and unrelated concepts in the Go programming ecosystem: 1. disintegration/imaging: This is a popular, lightweight Go package for image processing (e.g., resizing, cropping, rotation) [1][2][3]. It is not related to a Processor interface or Go context patterns. Its functions operate directly on types that satisfy the standard library's image.Image interface [1][4][5]. 2. Processor interface with context.Context: This is a common design pattern in Go for building extensible pipelines, workers, or middleware [6][7][8]. While not part of the standard library, it is frequently implemented using the following signature to ensure cancellation and timeout propagation [6][7][9]: type Processor interface { Process(ctx context.Context, r *Record) error } In this pattern, the context.Context is passed as the first argument to allow for graceful cancellation or deadline enforcement during the processing of a task [6][7][8]. Developers often use a functional adapter (e.g., ProcessorFunc) to allow simple functions to satisfy this interface, similar to the standard library's http.HandlerFunc [7]. If you were looking for a specific library that combines these, it is likely that you are using a custom wrapper or a different framework (such as Benthos or a background worker library) that utilizes the Processor interface pattern to handle image processing tasks or other data operations [7][8]. Citations:
🌐 Web query:
💡 Result: The sapphi-red/midec library is a pure Go multi-image (animated image) detector that supports formats such as GIF, APNG, WebP, and HEIF/AVIF [1][2]. To check if an image is animated, you use the IsAnimated function [1][2]. The usage pattern is as follows: import ( "fmt" "os" "github.com/sapphi-red/midec" _ "github.com/sapphi-red/midec/webp" // import to enable Animated WebP detection) func main { fp, _:= os.Open("image.webp") isAnimated:= midec.IsAnimated(fp) fmt.Println(isAnimated) } To add support for a new format or to register a specific handler, you use the midec.RegisterFormat function [1][2]. This function behaves similarly to the standard library's image.RegisterFormat and is typically called within an init function to register a format's header signature and its corresponding detection function [1][2]. Example: func init { midec.RegisterFormat("webp", webpHeader, isAnimated) } The library is designed with a pluggable architecture, allowing users to import specific sub-packages (e.g., _ "github.com/sapphi-red/midec/webp") to register and enable detection for that specific image format [1][2]. Citations: 🌐 Web query:
💡 Result: In the context of the traQ (traP Internal Messenger Application) backend, Citations:
🌐 Web query:
💡 Result: In the traQ backend codebase, SaveUploadStampImage is a function responsible for handling the image processing and saving logic when a new stamp is uploaded [1]. It is not a public API endpoint, but rather an internal utility function within the server-side implementation [1]. It is often invoked in contexts where an image file needs to be processed and stored as a stamp [1]. The function receives a context.Context as an argument, which is standard practice in the traQ Go backend to handle request-scoped values, cancellations, and deadlines across asynchronous operations or service calls [2][3]. The function is closely related to other image-handling routines and has been updated in the past to support different image formats, such as WebP, specifically for stamps [1]. Because it is an internal backend function, developers interacting with the traQ system typically use the high-level API methods provided by the official SDKs (such as ChangeStampImage or CreateStamp) rather than calling SaveUploadStampImage directly [2][3]. These high-level API methods also accept a context.Context to maintain proper request management [2]. Citations:
🌐 Web query:
💡 Result: The terms mentioned relate to Go-based image processing components often used in the context of the traQ messenger application or similar projects associated with Digital Creators Club traP. midec refers to a library (often seen as sapphi-red/midec or its WebAssembly port node-midec) that functions as a multi-image detector capable of identifying formats like Animated GIF, APNG, Animated WebP, and Animated HEIF/AVIF [1]. nativewebp is a pure Go implementation of a WebP encoder that does not require external C libraries like libwebp [2][3]. Within the nativewebp package, nativewebp.Encode is the primary function used to encode a single image into WebP format [2][4]. The package also supports animations through functions like EncodeAll, which handles multi-frame WebP files [2][3][5]. traQ is the open-source internal messenger application developed by the Digital Creators Club traP, written primarily in Go [6][7]. Components like image processing libraries are frequently integrated into such systems to handle media uploads and previews [6]. While these components may be used together in a software stack, there is no single standardized function named midec.IsAnimated that directly calls nativewebp.Encode in a public, monolithic API. Rather, midec is typically used to detect image characteristics, and nativewebp is used to encode/transcode them [2][1]. Developers often integrate these tools to perform tasks such as detecting animated media and subsequently re-encoding them into an optimized format like WebP [2][1][5]. Citations:
🌐 Web query:
💡 Result: The filename process_image.go is not a standard library component in the Go programming language, nor is it part of a universally defined framework. Instead, it is a common naming convention used by developers in specific Go-based projects to encapsulate image manipulation logic [1]. In software projects using this filename, process_image.go typically resides within utility or service directories (e.g., /router/utils/ or /internal/utils/) and is responsible for handling tasks such as: 1. Image Format Validation: Checking MIME types or file extensions to ensure the image meets project requirements [1]. 2. Processing Pipelines: Implementing logic for resizing, converting between formats (e.g., WebP to PNG), or compressing images [1]. 3. Metadata Management: Validating image properties or updating metadata during the upload or storage lifecycle [1]. 4. Error Handling: Mapping specific image processing failures (e.g., unsupported format or invalid animation) to appropriate system or HTTP responses [1]. If you have encountered this file in a specific codebase, it is a custom implementation tailored to that application's requirements for managing image assets. Citations: Propagate the request context through image processing. Update 🤖 Prompt for AI AgentsSource: Path instructions |
||
|
|
||
| case consts.MimeImageGIF: | ||
| // リサイズ | ||
| b, err := p.FitAnimationGIF(src, maxImageSize, maxImageSize) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
blank import になってるけどこれは理由がある?なければ削除してほしい
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
削除しました