func (s *diskStorage) PutEmail(addr *webfist.EmailAddr, email *webfist.Email) error { emailRoot := s.emailRoot(addr) err := os.MkdirAll(emailRoot, 0755) if err != nil { return err } r, err := email.Encrypted() if err != nil { return err } enc, err := ioutil.ReadAll(r) if err != nil { return err } s1 := sha1.New() s1.Write(enc) addrKey := addr.HexKey() encSHA1 := fmt.Sprintf("%x", s1.Sum(nil)) email.SetEncSHA1(encSHA1) emailPath := filepath.Join(emailRoot, encSHA1) if err := ioutil.WriteFile(emailPath, enc, 0644); err != nil { return err } return s.touchRecent(addrKey, encSHA1) }
func (s *diskStorage) Emails(addr *webfist.EmailAddr) ([]*webfist.Email, error) { emailRoot := s.emailRoot(addr) file, err := os.Open(emailRoot) if os.IsNotExist(err) { return nil, nil } if err != nil { return nil, err } infoList, err := file.Readdir(-1) if err != nil { return nil, err } result := make([]*webfist.Email, len(infoList)) for i, info := range infoList { emailPath := filepath.Join(emailRoot, info.Name()) file, err := os.Open(emailPath) if err != nil { return nil, err } all, err := ioutil.ReadAll(addr.Decrypter(file)) if err != nil { return nil, err } email, err := webfist.NewEmail(all) if err != nil { return nil, err } result[i] = email } return result, nil }
func (s *diskStorage) emailRoot(addr *webfist.EmailAddr) string { return s.emailRootFromHex(addr.HexKey()) }