浏览代码

added publish code

bmallred 9 年之前
父节点
当前提交
7e0fd4f615
共有 2 个文件被更改,包括 48 次插入4 次删除
  1. 37 4
      account.go
  2. 11 0
      account_test.go

+ 37 - 4
account.go

@ -11,6 +11,10 @@ import (
11 11
	"time"
12 12
)
13 13
14
const (
15
	publishLayout = "200601021504"
16
)
17
14 18
func getAccounts() ([]Account, error) {
15 19
	file := UserFile
16 20
@ -179,8 +183,37 @@ func (a *Account) Add(content, filename string, file []byte) (string, error) {
179 183
	return "", nil
180 184
}
181 185
182
func (a *Account) Publish(title, content string, date time.Time) (string, error) {
183
	// TODO: Check if symlink already exists
184
	// TODO: Create symbolic link in "blog" directory referencing content directory
185
	return "", nil
186
func (a *Account) Publish(title, content string, date time.Time) error {
187
	valid, err := a.Validate()
188
	if err != nil {
189
		return err
190
	}
191
	if !valid {
192
		return errors.New("Account does not exist")
193
	}
194
195
	prettyTitle := strings.ToLower(strings.Replace(title, " ", "-", -1))
196
	baseDir := os.Getenv("LOOP_DATA")
197
	userDir := strings.ToLower(a.Username)
198
	s := string(filepath.Separator)
199
	contentPath := baseDir + s + userDir + s + "content" + s + content
200
	publishPath := baseDir + s + userDir + s + "blog" + s + date.Format(publishLayout) + "-" + prettyTitle
201
202
	// Check if the content exists
203
	if _, err := os.Stat(contentPath); os.IsNotExist(err) {
204
		return err
205
	}
206
207
	// Check if symlink already exists
208
	if _, err := os.Stat(publishPath); os.IsExist(err) {
209
		return err
210
	}
211
212
	// Create symbolic link in "blog" directory referencing content directory
213
	err = os.Symlink(contentPath, publishPath)
214
	if err != nil {
215
		return err
216
	}
217
218
	return nil
186 219
}

+ 11 - 0
account_test.go

@ -3,6 +3,7 @@ package main
3 3
import (
4 4
	"os"
5 5
	"testing"
6
	"time"
6 7
)
7 8
8 9
func TestGetAccounts(t *testing.T) {
@ -52,6 +53,16 @@ func TestAdd(t *testing.T) {
52 53
}
53 54
54 55
func TestPublish(t *testing.T) {
56
	a := Account{
57
		Username:   "guest",
58
		Passphrase: "guest",
59
	}
60
61
	ConfigureEnvironment()
62
	err := a.Publish("My first blog post", "thisshouldnotexist", time.Now())
63
	if err == nil {
64
		t.Error("Fake content should not be publishable")
65
	}
55 66
}
56 67
57 68
func TestDelete(t *testing.T) {