Browse Source

Added downloading photos via sd.mephi.ru/api/4

Dmitry Yu Okunev 2 years ago
parent
commit
324b801062
6 changed files with 73 additions and 0 deletions
  1. 2 0
      .gitignore
  2. 46 0
      app/controllers/people.go
  3. 16 0
      app/helpers/files.go
  4. 7 0
      app/init.go
  5. 1 0
      conf/routes
  6. 1 0
      public/public

+ 2 - 0
.gitignore

@@ -4,3 +4,5 @@ routes/
 conf/app.conf
 app/models/*_reform.go
 *_easyjson.go
+public/photos/securityDepartment/*
+log/

+ 46 - 0
app/controllers/people.go

@@ -3,7 +3,11 @@ package controllers
 import (
 	renderModels "devel.mephi.ru/dyokunev/cps-api/app/models/for_render"
 	asuModels "devel.mephi.ru/dyokunev/go-asu-models"
+	"devel.mephi.ru/dyokunev/cps-api/app/helpers"
+	"devel.mephi.ru/dyokunev/go-sdapi/sdApi4"
+	"fmt"
 	"github.com/revel/revel"
+	"os"
 )
 
 type People struct {
@@ -33,3 +37,45 @@ func (c People) Index(person asuModels.Person, q string, prepareFormulars bool)
 
 	return c.Render(renderModels.NewPeople(people))
 }
+
+func (c People) renderNoPhoto() revel.Result {
+	return c.Redirect("https://voip.mephi.ru/public/photos/no_photo.png")
+}
+
+func (c People) renderPhotoOf(empGUID int) revel.Result {
+	// https://sd.mephi.ru/api/4/get_photos.jpg?api_key=&e_id=
+
+	photo, err := sdApi4.GetPhoto(empGUID)
+	if err != nil {
+		return c.RenderError(err)
+	}
+
+	filePath := fmt.Sprintf("public/photos/securityDepartment/%v.jpg", empGUID)
+
+	absFilePath := "/home/site/gopath/src/devel.mephi.ru/dyokunev/cps-api/"+filePath
+
+	if _, err = os.Stat(absFilePath); os.IsNotExist(err) {
+		err = helpers.SaveBytesToFile(absFilePath, photo);
+	}
+	if err != nil {
+		return c.RenderError(err)
+	}
+
+	return c.Redirect("/"+filePath)
+}
+
+func (c People) GetPhoto(empGUID int) revel.Result {
+	if r := c.Init(); r != nil {
+		return r
+	}
+
+	me := c.GetMe()
+
+	allowedPeopleMap := me.Permissions.AllowedPeople.ToMap()
+	if allowedPeopleMap[empGUID] == nil {
+		return c.renderNoPhoto()
+	}
+
+	return c.renderPhotoOf(empGUID)
+}
+

+ 16 - 0
app/helpers/files.go

@@ -0,0 +1,16 @@
+package helpers
+
+import (
+	"os"
+)
+
+func SaveBytesToFile(filePath string, photo []byte) error {
+	f, err := os.Create(filePath)
+	if err != nil {
+		return err
+	}
+	defer f.Close()
+
+	_, err = f.Write(photo)
+	return err
+}

+ 7 - 0
app/init.go

@@ -6,6 +6,7 @@ import (
 	"devel.mephi.ru/dyokunev/cps-api/app/models"
 	acdir "devel.mephi.ru/dyokunev/go-acdir"
 	asuModels "devel.mephi.ru/dyokunev/go-asu-models"
+	sdApi4 "devel.mephi.ru/dyokunev/go-sdapi/sdApi4"
 	"encoding/base64"
 	"encoding/json"
 	"fmt"
@@ -262,6 +263,11 @@ func initCasClient() {
 	})
 }
 
+func initSdApi() {
+	key, _ := revel.Config.String("sdapi4.apiKey")
+	sdApi4.SetApiKey(key)
+}
+
 func initAcdir() {
 	key, _ := revel.Config.String("sdapi_acdir_key")
 	acdir.Init(key)
@@ -303,6 +309,7 @@ func init() {
 	revel.OnAppStart(initCasClient)
 	revel.OnAppStart(initAcdir)
 	revel.OnAppStart(initUserInfo)
+	revel.OnAppStart(initSdApi)
 }
 
 // HeaderFilter adds common security headers

+ 1 - 0
conf/routes

@@ -14,6 +14,7 @@ GET	/noop					App.NoOp
 GET	/events/				Events.Index
 GET	/units/					Units.Index
 GET	/people/				People.Index
+GET	/people/:empGUID/format/jpg		People.GetPhoto
 GET	/timesheetRows/				TimesheetRows.Index
 
 # Ignore favicon requests

+ 1 - 0
public/public

@@ -0,0 +1 @@
+.