forked from mtusi2026/mtusi2026lab5
Реализация на 5
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
// Package fault implements explicit, file-controlled barriers for the isolated lab.
|
||||
package fault
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/google/uuid"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Hit(ctx context.Context, point string, id uuid.UUID) {
|
||||
if os.Getenv("LAB5_FAULTS") != "1" {
|
||||
return
|
||||
}
|
||||
p := filepath.Join("/faults", point+"."+id.String())
|
||||
b, e := os.ReadFile(p)
|
||||
if e != nil {
|
||||
p = filepath.Join("/faults", point+".all")
|
||||
b, e = os.ReadFile(p)
|
||||
if e != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
hit, e := os.OpenFile(p+".hit", os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0666)
|
||||
if e != nil {
|
||||
return
|
||||
}
|
||||
_, _ = hit.WriteString(id.String() + "\n")
|
||||
_ = hit.Close()
|
||||
switch strings.TrimSpace(string(b)) {
|
||||
case "crash":
|
||||
_ = os.Remove(p)
|
||||
os.Exit(86)
|
||||
case "pause":
|
||||
for {
|
||||
if _, e = os.Stat(p); os.IsNotExist(e) {
|
||||
return
|
||||
}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-time.After(50 * time.Millisecond):
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func Error(point string, id uuid.UUID) error {
|
||||
if os.Getenv("LAB5_FAULTS") != "1" {
|
||||
return nil
|
||||
}
|
||||
b, e := os.ReadFile(filepath.Join("/faults", point+"."+id.String()))
|
||||
if e != nil {
|
||||
return nil
|
||||
}
|
||||
v := strings.TrimSpace(string(b))
|
||||
if v == "temporary" || v == "permanent" {
|
||||
return errors.New(v)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package imageprocessor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"image"
|
||||
_ "image/jpeg"
|
||||
_ "image/png"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"time"
|
||||
)
|
||||
|
||||
func run(ctx context.Context, args ...string) error {
|
||||
cmd := exec.CommandContext(ctx, "magick", args...)
|
||||
cmd.Env = append(os.Environ(), "MAGICK_MEMORY_LIMIT=256MiB", "MAGICK_MAP_LIMIT=0", "MAGICK_DISK_LIMIT=256MiB", "MAGICK_THREAD_LIMIT=1", "MAGICK_TIME_LIMIT=60")
|
||||
cmd.WaitDelay = time.Second
|
||||
if err := cmd.Run(); err != nil {
|
||||
if ctx.Err() != nil {
|
||||
return ctx.Err()
|
||||
}
|
||||
return fmt.Errorf("image conversion failed: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func Validate(ctx context.Context, path string) error {
|
||||
return run(ctx, "-regard-warnings", path, "null:")
|
||||
}
|
||||
|
||||
type Result struct {
|
||||
Name, Path, MIME string
|
||||
Width, Height int
|
||||
}
|
||||
|
||||
func fit(w, h, limit int) (int, int) {
|
||||
if w <= limit && h <= limit {
|
||||
return w, h
|
||||
}
|
||||
if w >= h {
|
||||
return limit, max(1, h*limit/w)
|
||||
}
|
||||
return max(1, w*limit/h), limit
|
||||
}
|
||||
func Process(ctx context.Context, source, dir string, version int) ([]Result, error) {
|
||||
normalized := filepath.Join(dir, "normalized.png")
|
||||
if e := run(ctx, source, "-auto-orient", "-background", "white", "-alpha", "remove", "-alpha", "off", "-strip", "png:"+normalized); e != nil {
|
||||
return nil, e
|
||||
}
|
||||
f, e := os.Open(normalized)
|
||||
if e != nil {
|
||||
return nil, e
|
||||
}
|
||||
cfg, _, e := image.DecodeConfig(f)
|
||||
f.Close()
|
||||
if e != nil {
|
||||
return nil, e
|
||||
}
|
||||
tw, th := fit(cfg.Width, cfg.Height, 256)
|
||||
ow, oh := fit(cfg.Width, cfg.Height, 1280)
|
||||
out := []Result{{"thumbnail", filepath.Join(dir, "thumbnail.jpg"), "image/jpeg", tw, th}, {"crop", filepath.Join(dir, "crop.jpg"), "image/jpeg", 128, 128}, {"optimized", filepath.Join(dir, "optimized.webp"), "image/webp", ow, oh}}
|
||||
for _, v := range out {
|
||||
args := []string{normalized}
|
||||
if v.Name == "crop" {
|
||||
side := min(cfg.Width, cfg.Height)
|
||||
args = append(args, "-gravity", "center", "-crop", fmt.Sprintf("%dx%d+0+0", side, side), "+repage")
|
||||
}
|
||||
args = append(args, "-resize", fmt.Sprintf("%dx%d!", v.Width, v.Height), "-strip", "-quality")
|
||||
quality := "85"
|
||||
if v.Name == "optimized" && version == 1 {
|
||||
quality = "80"
|
||||
}
|
||||
coder := "jpeg:"
|
||||
if v.Name == "optimized" {
|
||||
coder = "webp:"
|
||||
}
|
||||
args = append(args, quality, coder+v.Path)
|
||||
if e = run(ctx, args...); e != nil {
|
||||
return nil, e
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
@@ -67,3 +67,11 @@ func Missing(err error) bool {
|
||||
var api smithy.APIError
|
||||
return errors.As(err, &api) && (api.ErrorCode() == "NoSuchKey" || api.ErrorCode() == "NotFound" || api.ErrorCode() == "404")
|
||||
}
|
||||
|
||||
func (s *Store) List(ctx context.Context, token string) (*s3.ListObjectsV2Output, error) {
|
||||
q := &s3.ListObjectsV2Input{Bucket: &s.Bucket, MaxKeys: aws.Int32(100)}
|
||||
if token != "" {
|
||||
q.ContinuationToken = &token
|
||||
}
|
||||
return s.Client.ListObjectsV2(ctx, q)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user