2014-08-04 18:47:04 +00:00
|
|
|
package khepri
|
|
|
|
|
2014-09-21 14:33:20 +00:00
|
|
|
func (repo *Repository) Create(t Type, data []byte) (ID, error) {
|
|
|
|
// TODO: make sure that tempfile is removed upon error
|
2014-08-04 18:47:04 +00:00
|
|
|
|
2014-09-21 14:33:20 +00:00
|
|
|
// create tempfile in repository
|
2014-08-04 20:46:14 +00:00
|
|
|
var err error
|
2014-09-21 14:33:20 +00:00
|
|
|
file, err := repo.tempFile()
|
2014-08-04 20:46:14 +00:00
|
|
|
if err != nil {
|
2014-09-21 14:33:20 +00:00
|
|
|
return nil, err
|
2014-08-04 18:47:04 +00:00
|
|
|
}
|
|
|
|
|
2014-09-21 14:33:20 +00:00
|
|
|
// write data to tempfile
|
|
|
|
_, err = file.Write(data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-08-04 18:47:04 +00:00
|
|
|
}
|
|
|
|
|
2014-09-21 14:33:20 +00:00
|
|
|
// close tempfile, return id
|
|
|
|
id := IDFromData(data)
|
|
|
|
err = repo.renameFile(file, t, id)
|
2014-08-04 18:47:04 +00:00
|
|
|
if err != nil {
|
2014-09-21 14:33:20 +00:00
|
|
|
return nil, err
|
2014-08-04 18:47:04 +00:00
|
|
|
}
|
|
|
|
|
2014-09-21 14:33:20 +00:00
|
|
|
return id, nil
|
2014-08-04 18:47:04 +00:00
|
|
|
}
|