func GetAttachment(c *cli.Context) { objectId := helper.ValidId(c.Args().First()) file, err := data.Files().OpenId(bson.ObjectIdHex(objectId)) helper.ErrPanic(err) _, err = io.Copy(os.Stdout, file) helper.ErrPanic(err) err = file.Close() helper.ErrPanic(err) }
func CreateObject(c *cli.Context) { title := strings.Join(c.Args(), " ") object := data.CreateObject(title) err := data.Objects().Insert(object) helper.ErrPanic(err) fmt.Printf("%s\n", object.Id.Hex()) }
func AddAttachment(c *cli.Context) { objectId := helper.ValidId(c.Args().First()) attachmentPath := c.Args().Get(1) object, err := data.GetObject(objectId) helper.ErrExit(err != nil, fmt.Sprintf("Invalid object ID %s!\n", objectId)) dbFile, err := data.Files().Create("") helper.ErrPanic(err) file, err := os.Open(attachmentPath) helper.ErrPanic(err) defer file.Close() _, err = io.Copy(dbFile, file) helper.ErrPanic(err) err = dbFile.Close() helper.ErrPanic(err) var ( content = "" metadata = make(map[string]string) ) if strings.ToLower(filepath.Ext(file.Name())) == ".pdf" { if c.BoolT("extract-pdf-text") { content, metadata, err = helper.ConvertPDF(file) helper.ErrPanic(err) } } attachment := data.CreateAttachment(dbFile, path.Base(attachmentPath), content, metadata) object.Attachments = append(object.Attachments, *attachment) object.Update() fmt.Println(attachment.Id.Hex()) }
func DeleteAttachment(c *cli.Context) { objectId := helper.ValidId(c.Args().First()) err := data.Files().RemoveId(bson.ObjectIdHex(objectId)) helper.ErrPanic(err) query := data.Objects().Find(bson.M{ "attachments._id": bson.ObjectIdHex(objectId), }) result := []data.Object{} query.All(&result) for _, object := range result { object.RemoveAttachment(objectId) } }
func GetAttachmentInfo(c *cli.Context) { objectId := helper.ValidId(c.Args().First()) attachment, err := data.GetAttachment(objectId) helper.ErrPanic(err) metadata := make(map[string]string) for _, attribute := range attachment.MetaData { metadata[attribute.Name] = attribute.Value } keys := make([]string, 0, len(metadata)) for k := range metadata { keys = append(keys, k) } sort.Strings(keys) fmt.Printf("%s\n\n", colors.Bold(attachment.Filename)) fmt.Printf("%s\n\t%s\n", colors.Header("Uploaded"), attachment.UploadDate.Format(time.RFC1123)) for _, name := range keys { fmt.Printf("%s\n\t%v\n", colors.Header(name), metadata[name]) } }