func (r *Repo) Import(ctx context.Context, f io.Reader, reporter progress.ReportFunc) error {
	if reporter == nil {
		reporter = func(_, _ uint64, _ float64) {}
	}
	prog := progress.New(reporter)
	steps := prog.NewComponent()
	bundleDocs := prog.NewComponent()
	userDocs := prog.NewComponent()
	steps.Total(4)
	steps.Increment(1)
	udb, err := r.userDB(ctx)
	if err != nil {
		return err
	}
	pkg := &fb.Package{}
	if e := json.NewDecoder(f).Decode(pkg); e != nil {
		return errors.Wrap(e, "Unable to decode JSON")
	}
	steps.Increment(1)
	if e := pkg.Validate(); e != nil {
		return e
	}

	bundle := pkg.Bundle
	bundle.Owner = r.user
	if e := r.SaveBundle(ctx, bundle); e != nil {
		return e
	}
	steps.Increment(1)

	bdb, err := r.bundleDB(ctx, bundle)
	if err != nil {
		return err
	}
	steps.Increment(1)

	ctx, cancel := context.WithCancel(ctx)
	defer cancel()

	errCh := make(chan error)
	defer close(errCh)
	go func() {
		err := importBundleDocs(ctx, bdb, pkg, bundleDocs)
		if err != nil {
			cancel()
		}
		errCh <- err
	}()

	go func() {
		err := importUserDocs(ctx, udb, pkg, userDocs)
		if err != nil {
			cancel()
		}
		errCh <- err
	}()

	for i := 0; i < 2; i++ {
		if err := <-errCh; err != nil {
			return err
		}
	}

	log.Printf("Imported:\n%d Bundles\n%d Themes\n%d Decks\n%d Notes\n%d Cards\n",
		1, len(pkg.Themes), len(pkg.Decks), len(pkg.Notes), len(pkg.Cards))
	return nil
}
// Import imports a .fbb file and stores the content
Import imports a .fbb file and stores the content
// Import imports a . fbb file and stores the content
func (r *Repo) Import(ctx context.Context, f io.Reader, reporter progress.ReportFunc) error {
	if reporter == nil {
		reporter = func(_, _ uint64, _ float64) {}
	}
	prog := progress.New(reporter)
	steps := prog.NewComponent()
	bundleDocs := prog.NewComponent()
	userDocs := prog.NewComponent()
	steps.Total(4)
	steps.Increment(1)
	udb, err := r.userDB(ctx)
	if err != nil {
		return err
	}
	pkg := &fb.Package{}
	if e := json.NewDecoder(f).Decode(pkg); e != nil {
		return errors.Wrap(e, "Unable to decode JSON")
	}
	steps.Increment(1)
	if e := pkg.Validate(); e != nil {
		return e
	}
	bundle := pkg.Bundle
	bundle.Owner = r.user
	if e := r.SaveBundle(ctx, bundle); e != nil {
		return e
	}
	steps.Increment(1)
	bdb, err := r.bundleDB(ctx, bundle)
	if err != nil {
		return err
	}
	steps.Increment(1)
	ctx, cancel := context.WithCancel(ctx)
	defer cancel()
	errCh := make(chan error)
	defer close(errCh)
	go func() {
		err := importBundleDocs(ctx, bdb, pkg, bundleDocs)
		if err != nil {
			cancel()
		}
		errCh <- err
	}()
	go func() {
		err := importUserDocs(ctx, udb, pkg, userDocs)
		if err != nil {
			cancel()
		}
		errCh <- err
	}()
	for i := 0; i < 2; i++ {
		if err := <-errCh; err != nil {
			return err
		}
	}
	log.Printf("Imported:\n%d Bundles\n%d Themes\n%d Decks\n%d Notes\n%d Cards\n",
		1, len(pkg.Themes), len(pkg.Decks), len(pkg.Notes), len(pkg.Cards))
	return nil
}
func ( r * Repo ) Import ( ctx context . Context , f io . Reader , reporter progress . ReportFunc ) error { if reporter == nil { reporter = func ( _ , _ uint64 , _ float64 ) { } } prog := progress . New ( reporter ) steps := prog . NewComponent ( ) bundleDocs := prog . NewComponent ( ) userDocs := prog . NewComponent ( ) steps . Total ( 4 ) steps . Increment ( 1 ) udb , err := r . userDB ( ctx ) if err != nil { return err } pkg := & fb . Package { } if e := json . NewDecoder ( f ) . Decode ( pkg ) ; e != nil { return errors . Wrap ( e , " " ) } steps . Increment ( 1 ) if e := pkg . Validate ( ) ; e != nil { return e } bundle := pkg . Bundle bundle . Owner = r . user if e := r . SaveBundle ( ctx , bundle ) ; e != nil { return e } steps . Increment ( 1 ) bdb , err := r . bundleDB ( ctx , bundle ) if err != nil { return err } steps . Increment ( 1 ) ctx , cancel := context . WithCancel ( ctx ) defer cancel ( ) errCh := make ( chan error ) defer close ( errCh ) go func ( ) { err := importBundleDocs ( ctx , bdb , pkg , bundleDocs ) if err != nil { cancel ( ) } errCh <- err } ( ) go func ( ) { err := importUserDocs ( ctx , udb , pkg , userDocs ) if err != nil { cancel ( ) } errCh <- err } ( ) for i := 0 ; i < 2 ; i ++ { if err := <- errCh ; err != nil { return err } } log . Printf ( " \n \n \n \n \n \n " , 1 , len ( pkg . Themes ) , len ( pkg . Decks ) , len ( pkg . Notes ) , len ( pkg . Cards ) ) return nil }
Import imports a .fbb file and stores the content
Import imports a . fbb file and stores the content