Ejemplo n.º 1
0
func (p pg) Open(tx *postgres.DBTx, info FileInfo, txControl bool) (FileStream, error) {
	if info.Oid == nil {
		return nil, ErrNotFound
	}

	lo, err := tx.LargeObjects()
	if err != nil {
		return nil, err
	}
	obj, err := lo.Open(*info.Oid, pgx.LargeObjectModeRead)
	if err != nil {
		return nil, err
	}
	f := &pgFile{LargeObject: obj, size: info.Size}
	if txControl {
		f.tx = tx
	}
	return f, nil
}
Ejemplo n.º 2
0
func (p pg) Put(tx *postgres.DBTx, info FileInfo, r io.Reader, append bool) error {
	if !append {
		if err := tx.QueryRow("UPDATE files SET file_oid = lo_create(0) WHERE file_id = $1 RETURNING file_oid", info.ID).Scan(&info.Oid); err != nil {
			return err
		}
	}

	lo, err := tx.LargeObjects()
	if err != nil {
		return err
	}
	obj, err := lo.Open(*info.Oid, pgx.LargeObjectModeWrite)
	if err != nil {
		return err
	}
	if append {
		obj.Seek(info.Size, os.SEEK_SET)
	}
	if _, err := io.Copy(obj, r); err != nil {
		return err
	}

	return nil
}