Example #1
0
func GetScreenshotInfo(poolDir string, url string) *ScreenshotInfo {
	if !ppstrutil.IsValidURL(url) {
		return nil
	}

	ret := new(ScreenshotInfo)
	ret.Fingerprint = ppstrutil.URL2Fingerprint(url)
	ret.PoolDir = fmt.Sprintf("%s%s%s%s%s", poolDir, string(os.PathSeparator), ret.Fingerprint[0:2], string(os.PathSeparator), ret.Fingerprint[2:4])
	setupScreenshotInfo(ret)

	return ret
}
Example #2
0
func (this PuppeteerWebHandler) ServeHTTP(rsp http.ResponseWriter, req *http.Request) {
	if nil != req.Body {
		req.Body = http.MaxBytesReader(rsp, req.Body, BODY_MAX_SIZE)
		err := req.ParseMultipartForm(BODY_MAX_SIZE)

		if nil != err {
			rsp.WriteHeader(http.StatusRequestEntityTooLarge)
			return
		}
	}

	pathRegexp := regexp.MustCompile("^(\\/[a-zA-Z0-9\\-\\_]+\\/)([a-f0-9]{32}\\.[\\d]+)$")
	if "GET" == req.Method {
		if matchList := pathRegexp.FindStringSubmatch(req.URL.Path); nil != matchList {
			switch matchList[1] {
			case INFO_URI_PREFIX:
				if screenshotInfo := pppool.GetScreenshotInfoByFingerprint(gPuppeteerConf.PoolDir, matchList[2]); nil != screenshotInfo {
					apiResponse := PuppeteerWebAPIResponse{
						RetCode: API_RET_OK,
						RetMsg:  "",
						Data:    PuppeteerWebAPIInfo{Key: screenshotInfo.Fingerprint, Status: screenshotInfo.Status, LastUpdate: screenshotInfo.LastUpdate}}
					jsonBytes, _ := json.Marshal(apiResponse)

					rsp.Header().Set("Content-Type", "application/json")
					io.WriteString(rsp, string(jsonBytes))
				} else {
					rsp.WriteHeader(http.StatusBadRequest)
				}
				break
			case PIC_URI_PREFIX:
				if screenshotInfo := pppool.GetScreenshotInfoByFingerprint(gPuppeteerConf.PoolDir, matchList[2]); nil != screenshotInfo {
					if pppool.STAT_READY == screenshotInfo.Status {
						filePath := pppool.GetScreenshotFilePath(screenshotInfo)
						if fh, openErr := os.OpenFile(filePath, os.O_RDONLY, ppioutil.FILE_MASK); nil == openErr {
							rsp.Header().Set("Content-Type", "image/png")
							rsp.Header().Set("Content-Disposition", "inline; filename=screenshot.png")
							io.Copy(rsp, fh)
							fh.Close()
						} else {
							rsp.WriteHeader(http.StatusNotFound)
						}
					} else {
						rsp.WriteHeader(http.StatusNotFound)

					}
				} else {
					rsp.WriteHeader(http.StatusBadRequest)
				}
				break
			default:
				rsp.WriteHeader(http.StatusNotFound)
				break
			}
		} else {
			rsp.WriteHeader(http.StatusBadRequest)
		}
	} else if "POST" == req.Method {
		targetURL := req.FormValue(POST_PARAM_URL)
		userAgent := req.FormValue(POST_PARAM_UAGENT)

		if req.URL.Path == INFO_URI_PREFIX && "" != targetURL && "" != userAgent && ppstrutil.IsValidURL(targetURL) {
			fingerprint := ppstrutil.URL2Fingerprint(targetURL)
			screenshotInfo := pppool.GetScreenshotInfoByFingerprint(gPuppeteerConf.PoolDir, fingerprint)

			apiResponse := PuppeteerWebAPIResponse{}
			if nil != screenshotInfo {
				pppool.AppendScreenshotLog(screenshotInfo, fmt.Sprintf("%d\t%s\n", time.Now().Unix(), targetURL))
				jobData := map[string]string{ppqueue.URL: targetURL,
					ppqueue.TARGET_FILE: pppool.GetScreenshotFilePath(screenshotInfo),
					ppqueue.LOG_FILE:    pppool.GetScreenshotLogPath(screenshotInfo),
					ppqueue.USER_AGENT:  userAgent}
				if ppqueue.WriteJob(gPuppeteerConf.QueueDir, jobData) {
					apiResponse.RetCode = API_RET_OK
					apiResponse.Data = PuppeteerWebAPIInfo{Key: screenshotInfo.Fingerprint, Status: pppool.STAT_RUNNING, LastUpdate: 0}
				} else {
					apiResponse.RetCode = API_RET_ERR_IO
					apiResponse.RetMsg = API_RET_ERR_IO_MSG
					apiResponse.Data = PuppeteerWebAPIInfo{Key: screenshotInfo.Fingerprint, Status: pppool.STAT_RUNNING, LastUpdate: 0}
				}
			}
			jsonBytes, _ := json.Marshal(apiResponse)

			rsp.Header().Set("Content-Type", "application/json")
			io.WriteString(rsp, string(jsonBytes))
		} else {
			rsp.WriteHeader(http.StatusBadRequest)
		}
	}
}