Created
November 13, 2013 17:34
-
-
Save clone1018/7453065 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// validator | |
package main | |
import ( | |
"errors" | |
"image/jpeg" | |
"os" | |
"path/filepath" | |
"strconv" | |
) | |
var FRAME_PATH = filepath.Join("y:", "Web Product Photography", "001_RFU", "NonBrands") | |
func (f Frame) validate() (bool, []error) { | |
var errs []error | |
errs = append(errs, f.validateFolder()...) | |
errs = append(errs, f.validate360()...) | |
errs = append(errs, f.validateDescription()...) | |
errs = append(errs, f.validateFront()...) | |
errs = append(errs, f.validateVTO()...) | |
if errs != nil { | |
return false, errs | |
} | |
return true, errs | |
} | |
func (f Frame) validateFolder() []error { | |
var errs []error | |
fullPath := filepath.Join(FRAME_PATH, f.sku) | |
ext, err := exists(fullPath) | |
if err != nil || ext == false { | |
errs = append(errs, errors.New("Root Folder Not Found: "+fullPath)) | |
} | |
return errs | |
} | |
func (f Frame) validate360() []error { | |
var errs []error | |
for rot := 1; rot < 33; rot++ { | |
cNum := strPad(strconv.Itoa(rot)) | |
cImage := f.sku + "_" + cNum + ".jpg" | |
iPath := filepath.Join(FRAME_PATH, f.sku, "edits", cImage) | |
// Check that it exists | |
ext, err := exists(iPath) | |
if err != nil || ext == false { | |
errs = append(errs, errors.New("360 Not Found: "+iPath)) | |
} | |
// If file does exist validate it | |
if ext { | |
file, _ := os.Open(iPath) | |
defer file.Close() | |
info, err := jpeg.DecodeConfig(file) | |
if err != nil { | |
errs = append(errs, errors.New("360 Invalid Image: "+iPath)) | |
continue | |
} | |
if info.Height != 2848 || info.Width != 4272 { | |
errs = append(errs, errors.New("360 Wrong Size: "+iPath)) | |
} | |
} | |
} | |
return errs | |
} | |
func (f Frame) validateDescription() []error { | |
var errs []error | |
descFile := filepath.Join(FRAME_PATH, f.sku, f.sku+".txt") | |
ext, err := exists(descFile) | |
if err != nil || ext == false { | |
errs = append(errs, errors.New("Description Not Found: "+descFile)) | |
} | |
return errs | |
} | |
func (f Frame) validateFront() []error { | |
var errs []error | |
descFile := filepath.Join(FRAME_PATH, f.sku, "main.front.jpg") | |
ext, err := exists(descFile) | |
if err != nil || ext == false { | |
errs = append(errs, errors.New("Front Not Found: "+descFile)) | |
} | |
return errs | |
} | |
func (f Frame) validateVTO() []error { | |
var errs []error | |
descFile := filepath.Join(FRAME_PATH, f.sku, "edits", f.sku+"_vto.png") | |
ext, err := exists(descFile) | |
if err != nil || ext == false { | |
errs = append(errs, errors.New("Front Not Found: "+descFile)) | |
} | |
return errs | |
} | |
func strPad(number string) string { | |
if len(number) < 2 { | |
return "0" + number | |
} | |
return number | |
} | |
func exists(path string) (bool, error) { | |
_, err := os.Stat(path) | |
if err == nil { | |
return true, nil | |
} | |
if os.IsNotExist(err) { | |
return false, nil | |
} | |
return false, err | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment