Beispiel #1
0
func New(email, uri, content string) (*Job, error) {
	u, err := url.Parse(uri)
	if err != nil {
		blacklist.Blacklist(uri)
		return nil, BadUrlError
	}

	switch u.Scheme {
	case "http", "https":
		// Fine
	default:
		blacklist.Blacklist(uri)
		return nil, BadUrlError
	}

	query := u.Query()
	for _, param := range ParamsToClean {
		query.Del(param)
	}
	u.RawQuery = query.Encode()
	uri = u.String()

	if blacklist.IsBlacklisted(uri) {
		return nil, BlacklistedUrlError
	}

	key, err := uuid.NewV4()
	if err != nil {
		return nil, NoKeyError
	}

	j := &Job{
		Content:   content,
		Title:     uri,
		Email:     email,
		Url:       uri,
		Key:       key,
		Doc:       nil,
		Author:    DefaultAuthor,
		StartedAt: time.Now(),
	}

	err = os.MkdirAll(j.Root(), 0755)
	if err != nil {
		return nil, NoDirectoryError
	}

	return j, nil
}
Beispiel #2
0
func (e *Emailer) Process(job J.Job) {
	job.Progress("Sending to your Kindle...")

	defer e.wg.Done()
	if st, err := os.Stat(job.MobiFilePath()); err != nil {
		e.error(job, FriendlyMessage, "Something weird happened. Mobi is missing: %s", err)
		return
	} else {
		if st.Size() > MaxAttachmentSize {
			blacklist.Blacklist(job.Url)
			e.error(job, "Sorry, this article is too big to send!", "Attachment was too big (%d bytes)", st.Size())
			return
		}
	}

	m := &postmark.Message{
		From:     e.from,
		To:       job.Email,
		Subject:  Subject,
		TextBody: fmt.Sprintf("Straight to your Kindle! %s: %s", job.Title, job.Url),
	}

	if err := m.Attach(job.MobiFilePath()); err != nil {
		e.error(job, FriendlyMessage, "failed attaching file: %s", err)
		return
	}

	resp, err := e.postmark.Send(m)
	if resp == nil {
		e.error(job, FriendlyMessage, "failed sending email: %s", err)
		return
	}

	switch resp.ErrorCode {
	case 0:
		// All is well
	case 422:
		e.error(job, FriendlyMessage, "failed sending email: %s: %s", err, resp.Message)
		return
	case 300:
		e.error(job, "Your email appears invalid. Please try carefully remaking the bookmarklet.", "emailer: Email inactive or invalid")
		return
	case 406:
		e.error(job, "Your email appears to have bounced. Amazon likes to bounce emails sometimes, and my provider 'deactivates' the email. For now, try changing your Personal Documents Email. I'm trying to find a proper solution for this :(", "emailer: Email inactive or invalid")
		return
	default:
		e.error(job, FriendlyMessage, "Something bizarre happened with Postmark: %s", resp.Message)
		return
	}

	job.Progress("All done! Grab your Kindle and hang tight!")
	cache.Set(resp.MessageID, job.Url, OneHour)
	recordDurationStat(job)
	e.Output <- job
}