Ejemplo n.º 1
0
// Update Albums Meta by Id
func updateMeta(albumId int) error {
	a, ok := Albums[albumId]
	if !ok {
		return errors.New("Unknown AlbumId for meta update: " + strconv.Itoa(albumId))
	}

	var err error
	if a.MetaInfo, err = model.AlbumMeta(a.AlbumId); err != nil {
		return err
	} else if err := model.UpdateMeta(a); err != nil {
		return err
	}

	return nil
}
Ejemplo n.º 2
0
// Sync Album with photos.
// Can be partial or full
func syncAlbum(si *SyncItem) {
	if si.FullSync {
		log.Printf("Going full fs sync for album %s\n", si.Album.Alias)
	} else {
		log.Printf("Going partial sync for album %s\n", si.Album.Alias)
	}

	index := make(map[string]*model.Photo)
	for _, p := range si.Album.Photos {
		index[p.OriginalName] = p
	}

	var exists, nextId int
	var newPhotos model.PhotoList

	if si.FullSync {
		nextId = si.Album.Photos.MaxPhotoId() + 1
	} else {
		nextId, _ = si.Album.MaxFilename()
		nextId++
	}

	// critical section???
	for _, p := range si.FsPhotos {
		_, ok := index[p.OriginalName]
		if !ok {
			log.Println("Found new photo " + p.OriginalName)
			FillPhoto(si.Album, p, nextId)
			if err := CreatePhotos(si.Album, p); err != nil {
				log.Println(err)
				continue
			}

			newPhotos = append(newPhotos, p)
			nextId++
		} else {
			exists++
		}
	} //eof

	if len(newPhotos) > 0 {
		tx, err := model.DB().Begin()
		if err != nil {
			log.Println(err)
		}
		if err := model.AddPhotos(newPhotos); err != nil {
			log.Println(err)
			if err = tx.Rollback(); err != nil {
				log.Println(err)
			}
		} else {
			// Update Meta
			if si.Album.MetaInfo, err = model.AlbumMeta(si.Album.AlbumId); err != nil {
				log.Println(err)
			} else if err := model.UpdateMeta(si.Album); err != nil {
				log.Println(err)
			}

			// Commit transaction
			if err = tx.Commit(); err != nil {
				log.Println(err)
			}
		}
	}

	log.Printf("Sync Done. %d new, %d exists\n", len(newPhotos), exists)
}