Browse Source

added ability to add content

bmallred 9 years ago
parent
commit
759c380304
3 changed files with 76 additions and 8 deletions
  1. 50 6
      account.go
  2. 18 0
      account_test.go
  3. 8 2
      main.go

+ 50 - 6
account.go

@ -3,8 +3,8 @@ package main
3 3
import (
4 4
	"encoding/json"
5 5
	"errors"
6
	"fmt"
6 7
	"io/ioutil"
7
	"log"
8 8
	"os"
9 9
	"path/filepath"
10 10
	"strings"
@ -151,7 +151,6 @@ func (a *Account) Delete() error {
151 151
	if err != nil {
152 152
		return err
153 153
	}
154
	log.Printf(string(marshalled))
155 154
156 155
	// Write to the file
157 156
	fi, err := os.OpenFile(UserFile, os.O_TRUNC|os.O_WRONLY, 0666)
@ -177,10 +176,55 @@ func (a *Account) Delete() error {
177 176
}
178 177
179 178
func (a *Account) Add(content, filename string, file []byte) (string, error) {
180
	// TODO: Create new content directory
181
	// TODO: Check if filename already exists
182
	// TODO: Write file to content directory
183
	return "", nil
179
	if filename == "" {
180
		return "", errors.New("Missing filename")
181
	}
182
183
	valid, err := a.Validate()
184
	if err != nil {
185
		return "", err
186
	}
187
	if !valid {
188
		return "", errors.New("Account does not exist")
189
	}
190
191
	// Get basic information needed
192
	baseDir := os.Getenv("LOOP_DATA")
193
	userDir := strings.ToLower(a.Username)
194
	s := string(filepath.Separator)
195
196
	// Create new content directory if necessary
197
	if content == "" {
198
		content = fmt.Sprintf("%x", hash(userDir+time.Now().Format(publishLayout), salt()))
199
	}
200
201
	// Create new content directory
202
	contentPath := baseDir + s + userDir + s + "content" + s + content
203
	if _, err := os.Stat(contentPath); os.IsNotExist(err) {
204
		err = os.MkdirAll(contentPath, 0755)
205
		if err != nil {
206
			return "", err
207
		}
208
	}
209
210
	// Check if filename already exists
211
	filePath := contentPath + s + filename
212
	if _, err := os.Stat(filePath); os.IsExist(err) {
213
		return filePath, err
214
	}
215
216
	// Write file to content directory
217
	fi, err := os.OpenFile(filePath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
218
	if err != nil {
219
		return filePath, err
220
	}
221
	_, err = fi.Write(file)
222
	if err != nil {
223
		return filePath, err
224
	}
225
	fi.Close()
226
227
	return filePath, nil
184 228
}
185 229
186 230
func (a *Account) Publish(title, content string, date time.Time) error {

+ 18 - 0
account_test.go

@ -50,6 +50,24 @@ func TestValidate(t *testing.T) {
50 50
}
51 51
52 52
func TestAdd(t *testing.T) {
53
	a := Account{
54
		Username:   "guest",
55
		Passphrase: "guest",
56
	}
57
58
	ConfigureEnvironment()
59
	fp, err := a.Add("", "", []byte{})
60
	if err == nil {
61
		t.Error("Fake content should not be publishable")
62
	}
63
64
	fp, err = a.Add("", "README.md", []byte{})
65
	if fp == "" {
66
		t.Error("File name should have been returned")
67
	}
68
	if err != nil {
69
		t.Error(err)
70
	}
53 71
}
54 72
55 73
func TestPublish(t *testing.T) {

+ 8 - 2
main.go

@ -3,6 +3,7 @@ package main
3 3
import (
4 4
	"crypto/aes"
5 5
	"crypto/cipher"
6
	"crypto/md5"
6 7
	"crypto/rand"
7 8
	"crypto/sha512"
8 9
	"encoding/base64"
@ -119,7 +120,12 @@ func decrypt(text []byte, passphrase string) ([]byte, error) {
119 120
	decrypter.XORKeyStream(data, data)
120 121
	return data, nil
121 122
}
122
func hash(username, passphrase, salt string) []byte {
123
func hash(clearText, salt string) []byte {
124
	h := md5.New()
125
	h.Write([]byte(clearText))
126
	return h.Sum(nil)
127
}
128
func hashCredentials(username, passphrase, salt string) []byte {
123 129
	clearText := fmt.Sprintf(
124 130
		"%s%s-%s",
125 131
		salt,
@ -131,5 +137,5 @@ func hash(username, passphrase, salt string) []byte {
131 137
	return sha.Sum(nil)
132 138
}
133 139
func generatePassphrase(username, passphrase string) string {
134
	return encodeBase64(hash(username, passphrase, salt()))
140
	return encodeBase64(hashCredentials(username, passphrase, salt()))
135 141
}