Exemple #1
0
func (h *handle) findRecord(w http.ResponseWriter, r *http.Request) (rec record.Record, matched bool) {
	h.RLock()
	defer h.RUnlock()

	// 1. try remote server via media-url
	// 2. try virtual image configuration from TOML
	// 3. try file map from scanned directory and the generated CSV file
	// 4. try to find a pattern in the URL which might look like an image size: 420x230
	// 5. print not found

	var path = r.URL.Path[1:]
	dir, file := filepath.Split(path)

	if vis, ok := h.virtualImages[dir]; ok {
		for _, vi := range vis {

			if vi.regex != nil {
				matched = vi.regex.MatchString(file)
				if matched {
					rec = record.NewRecordFields(h.pattern, path, vi.Width, vi.Height)
					return
				}

			} else {
				matched = true
				rec = record.NewRecordFields(h.pattern, path, vi.Width, vi.Height)
				return
			}
		}
	}

	rec, matched = h.fileMap[path]
	if !matched && len(path) > 0 && path[0] != '/' {
		rec, matched = h.fileMap["/"+path]
	}

	if false == matched {
		if false == common.IsImage(path) {
			return
		}

		if width, height := common.FileSizeFromPath(path); width > 0 && height > 0 {
			matched = true
			rec = record.NewRecordFields(h.pattern, path, width, height)
		}
		return
	}

	return
}
Exemple #2
0
func newHandle(ctx *cli.Context) *handle {
	csvFile := ctx.String("csv")
	var rec [][]string
	if csvFile != "" {
		rec = record.GetCSVContent(csvFile)
	}

	h := &handle{
		proxy:   newProxy(ctx),
		fileMap: make(map[string]record.Record),
		pattern: ctx.String("img-pattern"),
	}
	h.Lock()
	defer h.Unlock()

	if vif := ctx.String("img-config"); vif != "" {
		var err error
		if h.virtualImages, err = parseVirtualImageConfigFile(vif); err != nil {
			common.InfoErr("File %s contains error:\n%s\n", vif, err)
		}
	}

	h.fileMap["favicon.ico"] = record.NewRecordFields("icon", "favicon.ico", 16, 16)
	for _, row := range rec {
		rec, err := record.NewRecord(h.pattern, row...)
		if err != nil {
			common.InfoErr("File %s contains error: %s\n", csvFile, err)
		}
		rec.Path = ctx.String("url-prefix") + rec.Path
		h.fileMap[rec.Path] = rec
	}
	h.length = len(rec)
	return h
}