-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
225 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -104,4 +104,4 @@ nfpm: | |
recommends: | ||
- tig | ||
empty_folders: | ||
- /var/lib/drm | ||
- /var/local/lib/drm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package blobs | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
"time" | ||
|
||
"github.com/lylex/drm/pkg/files" | ||
"github.com/lylex/drm/pkg/utils" | ||
"github.com/spf13/viper" | ||
"github.com/tidwall/buntdb" | ||
) | ||
|
||
const ( | ||
MaxRandomNumber int = 10000 | ||
|
||
MetaDataDB string = "data.db" | ||
|
||
CfgDataPathKey = "dataPath" | ||
) | ||
|
||
type Blob struct { | ||
FileName string | ||
Dir string | ||
|
||
// CreatedAt represents when the blob is created, i.e. when it is deleted. | ||
CreatedAt time.Time | ||
} | ||
|
||
// Create make a blob metadate. | ||
func Create(fullPath string) *Blob { | ||
return &Blob{ | ||
FileName: files.Name(fullPath), | ||
Dir: files.Dir(fullPath), | ||
CreatedAt: time.Now(), | ||
} | ||
} | ||
|
||
func (b *Blob) Name() string { | ||
return fmt.Sprintf("%d_%4d_%s", | ||
b.CreatedAt.UnixNano(), | ||
rand.Intn(MaxRandomNumber), | ||
b.FileName) | ||
} | ||
|
||
// TODO | ||
func (b *Blob) Save() error { | ||
db, err := buntdb.Open(viper.GetString(CfgDataPathKey) + MetaDataDB) | ||
if err != nil { | ||
return err | ||
} | ||
defer db.Close() | ||
|
||
var json string | ||
json, err = utils.Marshal(*b) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = db.Update(func(tx *buntdb.Tx) error { | ||
_, _, err := tx.Set(b.FileName, json, nil) | ||
return err | ||
}) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package utils | ||
|
||
import "testing" | ||
|
||
func TestMarshal(t *testing.T) { | ||
type ColorGroup struct { | ||
ID int | ||
Name string | ||
Colors []string | ||
} | ||
group := ColorGroup{ | ||
ID: 1, | ||
Name: "Reds", | ||
Colors: []string{"Crimson", "Red", "Ruby", "Maroon"}, | ||
} | ||
|
||
result, err := Marshal(group) | ||
expect := `{"ID":1,"Name":"Reds","Colors":["Crimson","Red","Ruby","Maroon"]}` | ||
|
||
if result != expect { | ||
t.Errorf("failed to marshal object, expect %v, got %v", expect, result) | ||
} | ||
if err != nil { | ||
t.Errorf("unexpected error occured: %v", err) | ||
} | ||
} | ||
|
||
func TestUnmarshal(t *testing.T) { | ||
type ColorGroup struct { | ||
ID int | ||
Name string | ||
Colors []string | ||
} | ||
group := ColorGroup{} | ||
str := `{"ID":1,"Name":"Reds","Colors":["Crimson","Red","Ruby","Maroon"]}` | ||
|
||
err := Unmarshal(str, &group) | ||
expect := ColorGroup{ | ||
ID: 1, | ||
Name: "Reds", | ||
Colors: []string{"Crimson", "Red", "Ruby", "Maroon"}, | ||
} | ||
|
||
if group.Name != expect.Name || group.ID != expect.ID { | ||
t.Errorf("failed to unmarshal string to object: expect %v, got %v", expect, group) | ||
} | ||
if err != nil { | ||
t.Errorf("unexpected error occured: %v", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
{ | ||
"dataPath": "/usr/local/lib/drm/" | ||
"dataPath": "/usr/local/lib/drm/", | ||
"blobPath": "/usr/local/lib/drm/blob/" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
#!/bin/bash -ex | ||
|
||
echo "preinstall drm" | ||
CFG_DIR=/etc/drm | ||
DATA_DIR=/usr/local/lib/drm | ||
BLOB_DIR=$DATA_DIR/blob | ||
|
||
[ -d /etc/drm ] || mkdir /etc/drm | ||
[ -d $CFG_DIR ] || mkdir $CFG_DIR | ||
|
||
if [ ! -d /var/lib/drm ]; then | ||
mkdir /var/lib/drm | ||
chmod -R 666 /var/lib/drm | ||
if [ ! -d $DATA_DIR ]; then | ||
mkdir $DATA_DIR $BLOB_DIR | ||
chmod -R 777 $DATA_DIR | ||
fi |