Skip to content
This repository has been archived by the owner on Jul 2, 2024. It is now read-only.

Commit

Permalink
fix directory creation (fixes #6)
Browse files Browse the repository at this point in the history
  • Loading branch information
juruen committed Feb 25, 2018
1 parent e3c68b7 commit 93d8057
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 18 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## rmapi master

* Fix directory creation (fixes #6)

* Add stat command to show entry's metadata

## rmapi 0.0.1 (February 24, 2018)

* Initial release with support for most of the API and autocompletion.
Expand Down
53 changes: 44 additions & 9 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,24 +94,59 @@ func (ctx *ApiCtx) FetchDocument(docId, dstPath string) error {
_, err = util.CopyFile(tmpPath, dstPath)

if err != nil {
log.Error.Printf("failed to copy %s to %s, er: %s\n", tmpPath, dstPath, err.Error())
log.Error.Printf("failgied to copy %s to %s, er: %s\n", tmpPath, dstPath, err.Error())
return err
}

return nil
}

func (ctx *ApiCtx) CreateDir(parentId, name string) (model.Document, error) {
metaDoc := model.CreateDirDocument(parentId, name)
uploadRsp, err := ctx.uploadRequest(model.DirectoryType)

err := ctx.Http.Put(transport.UserBearer, updateStatus, metaDoc, nil)
if err != nil {
return model.Document{}, err
}

if !uploadRsp.Success {
return model.Document{}, errors.New("upload request returned success := false")
}

zippath, err := util.CreateZipDirectory(uploadRsp.ID)

if err != nil {
log.Error.Println("failed to create a new device directory", err)
log.Error.Println("failed to create zip directory", err)
return model.Document{}, err
}

return metaDoc.ToDocument(), nil
f, err := os.Open(zippath)
defer f.Close()

if err != nil {
log.Error.Println("failed to read zip file to upload", zippath, err)
return model.Document{}, err
}

err = ctx.Http.PutStream(transport.UserBearer, uploadRsp.BlobURLPut, f)

if err != nil {
log.Error.Println("failed to upload directory", err)
return model.Document{}, err
}

metaDoc := model.CreateUploadDocumentMeta(uploadRsp.ID, model.DirectoryType, parentId, name)

err = ctx.Http.Put(transport.UserBearer, updateStatus, metaDoc, nil)

if err != nil {
log.Error.Println("failed to move entry", err)
return model.Document{}, err
}

doc := metaDoc.ToDocument()

return doc, err

}

func (ctx *ApiCtx) DeleteEntry(node *model.Node) error {
Expand Down Expand Up @@ -159,7 +194,7 @@ func (ctx *ApiCtx) UploadDocument(parent string, pdfpath string) (*model.Documen
return nil, errors.New("file name is invalid")
}

uploadRsp, err := ctx.uploadRequest()
uploadRsp, err := ctx.uploadRequest(model.DocumentType)

if err != nil {
return nil, err
Expand Down Expand Up @@ -191,7 +226,7 @@ func (ctx *ApiCtx) UploadDocument(parent string, pdfpath string) (*model.Documen
return nil, err
}

metaDoc := model.CreateUploadDocumentMeta(uploadRsp.ID, parent, name)
metaDoc := model.CreateUploadDocumentMeta(uploadRsp.ID, model.DirectoryType, parent, name)

err = ctx.Http.Put(transport.UserBearer, updateStatus, metaDoc, nil)

Expand All @@ -205,8 +240,8 @@ func (ctx *ApiCtx) UploadDocument(parent string, pdfpath string) (*model.Documen
return &doc, err
}

func (ctx *ApiCtx) uploadRequest() (model.UploadDocumentResponse, error) {
uploadReq := model.CreateUploadDocumentRequest()
func (ctx *ApiCtx) uploadRequest(entryType string) (model.UploadDocumentResponse, error) {
uploadReq := model.CreateUploadDocumentRequest(entryType)
uploadRsp := make([]model.UploadDocumentResponse, 0)

err := ctx.Http.Put(transport.UserBearer, uploadRequest, uploadReq, &uploadRsp)
Expand Down
8 changes: 4 additions & 4 deletions model/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func CreateDirDocument(parent, name string) MetadataDocument {
}
}

func CreateUploadDocumentRequest() UploadDocumentRequest {
func CreateUploadDocumentRequest(entryType string) UploadDocumentRequest {
id, err := uuid.NewV4()

if err != nil {
Expand All @@ -82,18 +82,18 @@ func CreateUploadDocumentRequest() UploadDocumentRequest {

return UploadDocumentRequest{
id.String(),
DocumentType,
entryType,
1,
}
}

func CreateUploadDocumentMeta(id, parent, name string) MetadataDocument {
func CreateUploadDocumentMeta(id, entryType, parent, name string) MetadataDocument {

return MetadataDocument{
ID: id,
Parent: parent,
VissibleName: name,
Type: DocumentType,
Type: entryType,
Version: 1,
ModifiedClient: time.Now().Format(time.RFC3339Nano),
}
Expand Down
1 change: 0 additions & 1 deletion shell/arguments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@ func TestParseArguments(t *testing.T) {
assert.Equal(t, []string{"foo", "bar", "baz"}, parseArguments(" foo bar baz "))
assert.Equal(t, []string{"foo", "bar\\ baz"}, parseArguments(" foo bar\\ baz "))
assert.Equal(t, []string{"foo", "bar\\ baz", "bax"}, parseArguments(" foo bar\\ baz bax"))

}
33 changes: 29 additions & 4 deletions util/zipdoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/juruen/rmapi/log"
)

type content struct {
type zipDocumentContent struct {
ExtraMetadata map[string]string `json:"extraMetadata"`
FileType string `json:"fileType"`
LastOpenedPage int `json:"lastOpenedPage"`
Expand Down Expand Up @@ -64,7 +64,7 @@ func CreateZipDocument(id, srcPath string) (string, error) {
return "", err
}

c, err := createContent()
c, err := createZipContent()
if err != nil {
return "", err
}
Expand All @@ -74,8 +74,33 @@ func CreateZipDocument(id, srcPath string) (string, error) {
return tmp.Name(), nil
}

func createContent() (string, error) {
c := content{
func CreateZipDirectory(id string) (string, error) {
tmp, err := ioutil.TempFile("", "rmapizip")
log.Trace.Println("creating temp zip file:", tmp.Name())
defer tmp.Close()

if err != nil {
log.Error.Println("failed to create tmpfile for zip dir", err)
return "", err
}

w := zip.NewWriter(tmp)
defer w.Close()

// Create content content
f, err := w.Create(fmt.Sprintf("%s.content", id))
if err != nil {
log.Error.Println("failed to create content entry in zip file", err)
return "", err
}

f.Write([]byte("{}"))

return tmp.Name(), nil
}

func createZipContent() (string, error) {
c := zipDocumentContent{
make(map[string]string),
"pdf",
0,
Expand Down

0 comments on commit 93d8057

Please sign in to comment.