Esempio n. 1
0
File: main.go Progetto: cycps/addie
func dbUpdate(oid addie.Id, e addie.Identify) {
	log.Printf("[dbUpdate] %T '%s'", e, e.Identify())
	var err error = nil

	var old addie.Identify
	var ok bool

	if reflect.TypeOf(e).Name() == "Model" {
		old, ok = userModels[oid.Name]
		if !ok {
			log.Printf("[Update] bad oid %v\n", oid)
			return
		}
	} else {
		old, ok = design.Elements[oid]
		if !ok {
			log.Printf("[Update] bad oid %v\n", oid)
			return
		}
	}

	//todo perform check old.(type) conversion
	switch t := e.(type) {
	case addie.Computer:
		c := e.(addie.Computer)
		_, err = db.UpdateComputer(oid, old.(addie.Computer), c, user)
	case addie.Switch:
		s := e.(addie.Switch)
		_, err = db.UpdateSwitch(oid, old.(addie.Switch), s, user)
	case addie.Router:
		r := e.(addie.Router)
		_, err = db.UpdateRouter(oid, old.(addie.Router), r, user)
	case addie.Link:
		l := e.(addie.Link)
		_, err = db.UpdateLink(oid, l, user)
	case addie.Phyo:
		p := e.(addie.Phyo)
		_, err = db.UpdatePhyo(oid, p, user)
	case addie.Model:
		m := e.(addie.Model)
		err = db.UpdateModel(oid.Name, m, user)
	case addie.Sax:
		s := e.(addie.Sax)
		_, err = db.UpdateSax(oid, old.(addie.Sax), s, user)
	case addie.Plink:
		p := e.(addie.Plink)
		_, err = db.UpdatePlink(oid, p, user)
	default:
		log.Printf("[dbUpdate] unkown or unsupported element type: %T \n", t)
	}

	if err != nil {
		log.Println(err)
	}

}
Esempio n. 2
0
File: main.go Progetto: cycps/addie
func onModelIco(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {

	log.Println("addie receiving model icon")
	err := r.ParseMultipartForm(50 * 1024 * 1024)
	if err != nil {
		log.Println("parse form failed")
		log.Println(err)
	}
	mdl := r.MultipartForm.Value["modelName"][0]
	log.Printf("model: %s", mdl)
	log.Printf("file: %s", r.MultipartForm.File["modelIco"][0].Filename)

	f, err := r.MultipartForm.File["modelIco"][0].Open()
	if err != nil {
		log.Println("error opening icon file")
		log.Println(err)
	}

	content, err := ioutil.ReadAll(f)
	if err != nil {
		log.Println("could not read file")
		log.Println(err)
	}

	log.Printf("icon file size: %d", len(content))

	uuid := uuid.NewV4()
	fn := "/tmp/" + uuid.String() + "_" + mdl + ".png"
	m, ok := userModels[mdl]
	if ok {
		m.Icon = fn
		userModels[mdl] = m
		db.UpdateModel(m.Name, m, user)
	}

	log.Println("saving icon " + fn)
	err = ioutil.WriteFile(fn, content, 0644)
	if err != nil {
		log.Println(err)
		log.Println("failed to save icon file")
	}

	err = sshCmd("web", "mkdir -p /cypress/web/ico/"+user)
	if err != nil {
		log.Println(err)
		log.Println("could not create user model icon directory on webserver")
	}

	err = syncFile(fn, "web:/cypress/web/ico/"+user+"/"+mdl+".png")
	if err != nil {
		log.Println(err)
		log.Println("could not sync model icon to user icon dir on webserver")
	}

	err = os.Remove(fn)
	if err != nil {
		log.Printf("could not remove temporary icon file %s", fn)
	}

	//log.Println(r.MultipartForm)

}