Beispiel #1
0
func expired_uploads(database *box.Database, arguments []string) (err error) {

	// Open database transaction
	var transaction *box.Transaction

	transaction, err = database.BeginTransaction()
	if err != nil {
		return
	}

	defer transaction.Rollback()

	// Handle arguments
	switch len(arguments) {
	case 0:
		var ids []string

		// Execute the query
		ids, err = transaction.QueryExpiredUploads()
		if err != nil {
			return
		}

		// Print result
		for _, id := range ids {
			fmt.Printf("%s\n",
				id)
		}

	default:
		err = fmt.Errorf("To many arguments. Usage: box_util expired_uploads")
	}

	return
}
Beispiel #2
0
func space_consumption(database *box.Database, arguments []string) (err error) {

	// Open database transaction
	var transaction *box.Transaction

	transaction, err = database.BeginTransaction()
	if err != nil {
		return
	}

	defer transaction.Rollback()

	// Handle arguments
	switch len(arguments) {
	case 0:
		var space map[string]uint64

		// Execute the query
		space, err = transaction.QuerySpaceConsumption()
		if err != nil {
			return
		}

		// Print result
		for user, space := range space {
			fmt.Printf("%s %d\n",
				user,
				space)
		}

	case 1:
		var user string = arguments[0]
		var space uint64

		// Execute the query
		space, err = transaction.QuerySpaceConsumptionFor(user)
		if err != nil {
			return
		}

		// Print result
		fmt.Printf("%s %d\n",
			user,
			space)

	default:
		err = fmt.Errorf("To many arguments. Usage: box_util space_consumption [USER]")
	}

	return
}
Beispiel #3
0
func main() {
	var err error

	box.ParseFlags()

	// Box connections
	var (
		database *box.Database

		storage *box.Storage
	)

	// Connect to database
	database, err = box.ConnectDatabase()
	if err != nil {
		box.LogFatal("%v", err)
	}

	defer database.Close()

	// Connect to storage
	storage, err = box.ConnectStorage()
	if err != nil {
		box.LogFatal("%v", err)
	}

	defer storage.Close()

	// Open transaction
	var transaction *box.Transaction

	transaction, err = database.BeginTransaction()
	if err != nil {
		box.LogFatal("%v", err)
	}

	defer transaction.Rollback()

	// Find all expired uploads
	var ids []string

	// Get all ids in storage
	ids, err = storage.List()
	if err != nil {
		box.LogFatal("%v", err)
	}

	// Remove all expired uploads from storage
	for _, id := range ids {
		var expired bool

		// Query if the file is expired
		expired, err = transaction.QueryExpiredUpload(id)
		if err != nil {
			box.LogFatal("%v", err)
		}

		if expired {
			box.LogDebug("Removing file: %s", id)

			err = storage.Remove(id)
			if err != os.ErrNotExist {
				// File is already gone - ignore it
			} else if err != nil {
				box.LogError("%v", err)
			}
		}
	}
}