Beispiel #1
0
func mailToPdfZip(outfn, inpfn string, splitted bool, outimg string, imgsize string) error {
	input, err := openIn(inpfn)
	if err != nil {
		return err
	}
	defer func() { _ = input.Close() }()
	if !splitted && outimg == "" {
		return converter.MailToPdfZip(outfn, input, "message/rfc822", nil)
	}
	return converter.MailToSplittedPdfZip(outfn, input, "message/rfc822",
		splitted, outimg, imgsize, nil)
}
Beispiel #2
0
func emailConvertEP(ctx context.Context, request interface{}) (response interface{}, err error) {
	req := request.(emailConvertRequest)
	defer req.Input.Close()

	getOutFn := func(hsh string) string {
		return filepath.Join(converter.Workdir,
			fmt.Sprintf("result-%s!%s.zip", hsh, req.Params))
	}

	getCachedFn := func(hsh string) (string, error) {
		if strings.Contains(hsh, "/") {
			return "", fmt.Errorf("bad hsh: %q", hsh)
		}
		outFn := getOutFn(hsh)
		outFh, err := os.Open(outFn)
		if err != nil {
			return outFn, err
		}
		defer outFh.Close()
		defer func() {
			if err != nil {
				logger.Warn("msg", "removing stale result file "+outFn)
				_ = os.Remove(outFn)
			}
		}()

		fi, err := outFh.Stat()
		if err != nil || fi.Size() == 0 {
			return outFn, err
		}
		// test correctness of the zip file
		z, err := zip.OpenReader(outFh.Name())
		if err != nil {
			return outFn, err
		}
		_ = z.Close()
		return outFn, nil
	}
	resp := emailConvertResponse{
		r: ctx.Value("http.Request").(*http.Request),
	}

	for _, hsh := range req.IfNoneMatch {
		if _, err := getCachedFn(hsh); err == nil {
			resp.NotModified = true
			return resp, nil
		}
	}

	h := sha1.New()
	inpFn, err := readerToFile(io.TeeReader(req.Input, h), req.Input.Filename)
	if err != nil {
		return resp, fmt.Errorf("cannot read input file: %v", err)
	}
	if !converter.LeaveTempFiles {
		defer os.Remove(inpFn)
	}
	hsh := base64.URLEncoding.EncodeToString(h.Sum(nil))
	if resp.outFn, err = getCachedFn(hsh); err == nil {
		return resp, nil
	}

	input, err := os.Open(inpFn)
	if err != nil {
		return nil, err
	}

	if !req.Params.Splitted && req.Params.OutImg == "" {
		err = converter.MailToPdfZip(resp.outFn, input, req.Params.ContentType, nil)
	} else {
		err = converter.MailToSplittedPdfZip(resp.outFn, input, req.Params.ContentType,
			req.Params.Splitted, req.Params.OutImg, req.Params.ImgSize, nil)
	}
	if err != nil {
		logger.Error("msg", "MailToSplittedPdfZip", "dest", resp.outFn, "input", input, "params", req.Params, "error", err)
		return resp, err
	}
	return resp, nil
}