예제 #1
0
func MoveFile(location *location.Model, mediatype *mediatype.Model, srcPath string) (string, error) {

	// create directories if necessary
	dir := fmt.Sprintf("%s/%s/%s", cfg.Media.Root, location.StaticPath, mediatype.Name)
	err := os.MkdirAll(dir, 0755)
	if err != nil {
		log.Errorf("Unable to create directory: %s", err)
		return "", errors.New("Could not process uploaded file")
	}

	// generate filename randomly
	filename, err := random.New(16)
	if err != nil {
		log.Errorf("Unable to generate filename: %s", err)
		return "", errors.New("Could not process uploaded file")
	}
	dstPath := fmt.Sprintf("%s/%s", dir, filename)

	// move file to its destination
	err = os.Rename(srcPath, dstPath)
	if err != nil {
		log.Errorf("Could not move file %s", err)
		return "", errors.New("Could not process uploaded file")
	}

	return dstPath, nil
}
예제 #2
0
func createToken(db *pg.Session, userId int64) (*Model, error) {
	// generate key
	key, err := random.New(32)
	if err != nil {
		return nil, err
	}

	// create new reset token structure
	resetToken := &Model{
		Id:         null.NewInt(0, false),
		State:      ResetTokenActive,
		Key:        key,
		Expiration: time.Now().Add(time.Minute * 10),
		UserId:     userId,
	}

	// create new user in database
	err = db.Create(resetToken)
	if err != nil {
		return nil, fmt.Errorf("Could not persist reset token: %s", err)
	}

	return resetToken, nil
}