Exemplo n.º 1
0
func exportAttachment(id int64) error {

	// Split the filename and ensure the directory exists
	path, name := splitFilename(strconv.FormatInt(id, 10))
	path = config.Export.OutputDirectory + f.AttachmentsPath + path

	if !fileExists(path) {
		err := mkDirAll(path)
		if err != nil {
			return err
		}
	}

	filename := fmt.Sprintf("%s/%s.json", path, name)

	// Don't export if we've exported already
	if fileExists(filename) {
		return nil
	}

	vb := vbAttachment{}
	err := db.QueryRow(`
SELECT a.attachmentid
      ,a.dateline
      ,a.postid
      ,a.userid
      ,a.filename
      ,a.filesize
      ,a.filedata
      ,a.filehash
      ,a.extension
      ,t.mimetype
      ,a.visible
  FROM `+config.DB.TablePrefix+`attachment a
  JOIN `+config.DB.TablePrefix+`attachmenttype t ON t.extension = a.extension
 WHERE a.attachmentid = ?`,
		id,
	).Scan(
		&vb.AttachmentID,
		&vb.DateCreated,
		&vb.PostID,
		&vb.UserID,
		&vb.FileName,
		&vb.FileSize,
		&vb.FileData,
		&vb.FileHash,
		&vb.Extension,
		&vb.MimeType,
		&vb.Visible,
	)
	if err != nil {
		return err
	}

	ex := f.Attachment{}
	ex.ID = vb.AttachmentID
	ex.Author = vb.UserID
	ex.DateCreated = time.Unix(vb.DateCreated, 0).UTC()
	ex.Associations = append(ex.Associations, f.Association{
		OnType: "comment",
		OnID:   vb.PostID,
	})
	ex.Name = vb.FileName
	ex.ContentSize = int32(vb.FileSize)
	ex.MimeType = getMimeTypeFromFileName("name." + vb.Extension)
	ex.ContentURL = "data:" + ex.MimeType + ";base64," +
		base64.StdEncoding.EncodeToString(vb.FileData)

	err = writeFile(filename, ex)
	if err != nil {
		return err
	}

	exportedItemsLock.Lock()
	exportedItems.Files = append(exportedItems.Files, f.DirFile{
		ID:   ex.ID,
		Path: strings.Replace(filename, config.Export.OutputDirectory, "", 1),
	})
	exportedItemsLock.Unlock()

	return nil
}