Ejemplo n.º 1
0
// update user password with user id and new password
func UpdatePassword(id int64, newPassword string) error {
	u := new(User)
	u.Salt = utils.Md5String(newPassword)[8:24]
	u.Password = utils.Sha256String(newPassword + u.Salt)
	if _, err := vars.Db.Cols("password,salt").Where("id = ?", id).Update(u); err != nil {
		log.Error("Db|UpdatePassword|%d|%s", id, err.Error())
		return err
	}
	return nil
}
Ejemplo n.º 2
0
// new site init data
func NewSiteInitData(engine *xorm.Engine) {
	// default user
	user := &model.User{
		Name:      "admin",
		Nick:      "admin",
		Email:     "*****@*****.**",
		Url:       "#",
		AvatarUrl: utils.GravatarLink("*****@*****.**"),
		Profile:   "this is an administrator",
		Role:      model.USER_ROLE_ADMIN,
		Status:    model.USER_STATUS_ACTIVE,
	}
	user.Salt = utils.Md5String("123456789")[8:24]
	user.Password = utils.Sha256String("123456789" + user.Salt)
	if _, err := engine.Insert(user); err != nil {
		log.Error("NewSite | %s", err.Error())
		return
	}

	// default article
	article := &model.Article{
		Title:         "Welcome to Purine",
		Link:          "welcome-to-purine",
		Preview:       blogPreview,
		Body:          blogContent,
		TagString:     "blog",
		Hits:          1,
		Comments:      0,
		Status:        model.ARTICLE_STATUS_PUBLISH,
		CommentStatus: model.ARTICLE_COMMENT_OPEN,
		AuthorId:      user.Id,
	}
	if _, err := engine.Insert(article); err != nil {
		log.Error("NewSite | %s", err.Error())
		return
	}

	// default settings
	settings := make([]interface{}, 0)
	settings = append(settings, &model.Setting{"title", "Purine", 0})
	settings = append(settings, &model.Setting{"subtitle", "a simple blog engine", 0})
	settings = append(settings, &model.Setting{"desc", "a simple blog engine by golang", 0})
	settings = append(settings, &model.Setting{"keyword", "purine,blog,golang", 0})
	settings = append(settings, &model.Setting{"theme", "default", 0})
	settings = append(settings, &model.Setting{"baseurl", "http://localhost:9999/", 0})
	settings = append(settings, &model.Setting{"media_imageext", ".jpg,.jpeg,.png,.gif", 0})
	settings = append(settings, &model.Setting{"media_fileext", ".txt,.zip,.doc,.xls,.ppt,.pdf", 0})
	settings = append(settings, &model.Setting{"media_nameformat", ":hash", 0})
	settings = append(settings, &model.Setting{"media_maxsize", strconv.Itoa(2 * 1024 * 1024), 0})
	if _, err := engine.Insert(settings...); err != nil {
		log.Error("NewSite | %s", err.Error())
		return
	}
}
Ejemplo n.º 3
0
// create new token with user id and expiration duration
func CreateToken(user, expire int64) (*Token, error) {
	t := &Token{
		UserId:     user,
		ExpireTime: time.Now().Unix() + expire,
	}
	t.Token = utils.Md5String(fmt.Sprintf("%d,%d", t.UserId, t.ExpireTime))
	if _, err := vars.Db.Insert(t); err != nil {
		log.Error("Db|CreateToken|%v|%s", t, err.Error())
		return nil, err
	}
	return t, nil
}
Ejemplo n.º 4
0
// upload media
//
//  in  : *MediaUploadOption
//  out : {
//          "media":*Media
//        }
//
func (_ *MediaApi) Upload(v interface{}) *Res {
	meta, ok := v.(*MediaUploadOption)
	if !ok {
		return Fail(paramTypeError(meta))
	}
	res := Setting.ReadMedia(nil)
	if !res.Status {
		return res
	}
	setting := res.Data["media"].(*SettingMedia)
	f, h, err := meta.Ctx.Req().FormFile(meta.FormName)
	if err != nil {
		return Fail(err)
	}
	defer f.Close()

	// check file size
	size, err := getUploadFileSize(f)
	if err != nil {
		return Fail(err)
	}
	if size > setting.MaxSize {
		return Fail(ERR_MEDIA_LARGE)
	}

	// check ext
	ext := path.Ext(h.Filename)
	extRule := setting.FileExt
	if meta.IsImage {
		extRule = setting.ImageExt
	}
	if !strings.Contains(extRule, ext) {
		return Fail(ERR_MEDIA_WRONG_TYPE)
	}

	// hash file name, make dir
	now := time.Now()
	hashName := utils.Md5String(fmt.Sprintf("%d%s%d", meta.User.Id, h.Filename, now.UnixNano())) + ext
	fileName := path.Join("static/upload", hashName)
	fileDir := path.Dir(fileName)
	if !com.IsDir(fileDir) {
		if err = os.MkdirAll(fileDir, os.ModePerm); err != nil {
			return Fail(err)
		}
	}
	if err = meta.Ctx.SaveToFile(meta.FormName, fileName); err != nil {
		return Fail(err)
	}

	// save file media info
	m := &model.Media{
		Name:     h.Filename,
		FileName: hashName,
		FilePath: fileName,
		FileSize: size,
		FileType: h.Header.Get("Content-Type"),
		OwnerId:  meta.User.Id,
	}
	if err = model.SaveMedia(m); err != nil {
		return Fail(err)
	}

	return Success(map[string]interface{}{
		"media": m,
	})
}