Esempio n. 1
0
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)
}
Esempio n. 2
0
func DeleteObject(c *cli.Context) {
	objectId := helper.ValidId(c.Args().First())

	object, err := data.GetObject(objectId)
	helper.ErrExit(err != nil, fmt.Sprintf("Invalid object ID %s!\n", objectId))

	files := data.Files()
	for _, attachment := range object.Attachments {
		files.RemoveId(attachment.Id)
	}

	data.Objects().RemoveId(object.Id)
}
Esempio n. 3
0
func RemoveTag(c *cli.Context) {
	if len(c.Args()) < 2 {
		helper.ErrExit(true, "Not enough arguments provided")
	}

	objectId := helper.ValidId(c.Args().First())
	value := strings.Join(c.Args().Tail(), " ")

	object, err := data.GetObject(objectId)
	helper.ErrExit(err != nil, fmt.Sprintf("Invalid object ID %s!\n", objectId))

	object.RemoveTag(value)
}
Esempio n. 4
0
func GetObject(c *cli.Context) {
	colors.Allow(c)

	objectId := helper.ValidId(c.Args().First())

	object, err := data.GetObject(objectId)
	helper.ErrExit(err != nil, fmt.Sprintf("Invalid object ID %s!\n", objectId))

	attributes := make(map[string][]string)
	for _, attribute := range object.Attributes {
		attributes[attribute.Name] = append(attributes[attribute.Name], attribute.Value)
	}

	keys := make([]string, 0, len(attributes))
	for k := range attributes {
		keys = append(keys, k)
	}
	sort.Strings(keys)

	fmt.Printf("%s\n\n", colors.Bold(object.Title))
	fmt.Printf("%s\n\t%s\n", colors.Header("Created"), object.CreateDate.Format(time.RFC1123))

	if _, exists := attributes["content"]; exists {
		fmt.Printf("\n%s\n\t%s\n", colors.Header("Content"), strings.Join(attributes["content"], ", "))
		delete(attributes, "content")
	}

	if len(attributes) > 0 {
		fmt.Printf("\n%s\n", colors.Header("Attributes"))

		for k := range keys {
			sort.Strings(attributes[keys[k]])

			fmt.Printf("\t%s: %s\n", colors.Bold(keys[k]), strings.Join(attributes[keys[k]], ", "))
		}
	}

	if len(object.Attachments) > 0 {
		data.ClearCache()
		defer data.FlushCache()

		fmt.Printf("\n%s\n", colors.Header("Attachments"))

		for index, attachment := range object.Attachments {
			fmt.Printf("\t(%s) %s: %s (%s)\n", colors.ShortId(index+1), colors.ObjectId(attachment.Id.Hex()), attachment.Filename, attachment.UploadDate.Format(time.RFC1123))
			data.SetCache(strconv.Itoa(index+1), attachment.Id.Hex())
		}
	}
}
Esempio n. 5
0
func RemoveAttribute(c *cli.Context) {
	if len(c.Args()) < 3 {
		helper.ErrExit(true, "Not enough arguments provided")
	}

	objectId := helper.ValidId(c.Args().First())

	name := c.Args().Get(1)
	value := strings.Join(c.Args()[2:], " ")

	object, err := data.GetObject(objectId)
	helper.ErrExit(err != nil, fmt.Sprintf("Invalid object ID %s!\n", objectId))

	object.RemoveAttribute(name, value)
}
Esempio n. 6
0
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)
	}
}
Esempio n. 7
0
func GetInfo(c *cli.Context) {
	objectId := helper.ValidId(c.Args().First())

	_, err := data.GetObject(objectId)
	if err == nil {
		GetObject(c)
		return
	}

	_, err = data.GetAttachment(objectId)
	if err == nil {
		GetAttachmentInfo(c)
		return
	}

	helper.ErrExit(err != nil, fmt.Sprintf("Invalid object ID %s!\n", objectId))
}
Esempio n. 8
0
func ListAttachments(c *cli.Context) {
	colors.Allow(c)

	objectId := helper.ValidId(c.Args().First())

	object, err := data.GetObject(objectId)
	helper.ErrExit(err != nil, fmt.Sprintf("Invalid object ID %s!\n", objectId))

	if len(object.Attachments) > 0 {
		data.ClearCache()
		defer data.FlushCache()

		for index, attachment := range object.Attachments {
			fmt.Printf("(%s) %s \"%s\"\n", colors.ShortId(index+1), colors.ObjectId(attachment.Id.Hex()), attachment.Filename)
			data.SetCache(strconv.Itoa(index+1), attachment.Id.Hex())
		}
	}
}
Esempio n. 9
0
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())
}
Esempio n. 10
0
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])
	}
}