github.com/benoitkugler/goacve@v0.0.0-20201217100549-151ce6e55dc8/client/controllers/main_documents.go (about) 1 package controllers 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "net/http" 7 "os" 8 "path/filepath" 9 10 "github.com/benoitkugler/goACVE/server/core/apiserver" 11 rd "github.com/benoitkugler/goACVE/server/core/rawdata" 12 ) 13 14 // -------------------------------------------------------------- 15 // ---------------- Fonctionnalités partagées ------------------- 16 // -------------------------------------------------------------- 17 18 // LoadMiniatures demande au serveur les miniatures associés aux ids 19 // fournis. Devrait être utilisée dans une goroutine. 20 func LoadMiniatures(idDocs rd.Ids) (map[int64][]byte, error) { 21 var docs rd.ContenuDocuments 22 err := requete(apiserver.UrlDocumentsMiniatures, http.MethodPost, idDocs, &docs) 23 if err != nil { 24 return nil, err 25 } 26 out := make(map[int64][]byte, len(docs)) 27 for _, cd := range docs { 28 out[cd.IdDocument] = cd.Miniature 29 } 30 return out, nil 31 } 32 33 func (c *MainController) UpdateDocument(doc rd.Document) *rd.Document { 34 c.ShowStandard("Modifications du document...", true) 35 var out rd.Document 36 err := requete(apiserver.UrlDocuments, http.MethodPost, doc, &out) 37 if err != nil { 38 c.ShowError(err) 39 return nil 40 } 41 c.Base.Documents[out.Id] = out 42 c.ShowStandard("Document mis à jour avec succès.", false) 43 return &out 44 } 45 46 // SupprimeDocument supprime le document de manière synchrone 47 func (c *MainController) SupprimeDocument(id int64) { 48 c.ShowStandard("Suppression du document...", true) 49 var out rd.Contraintes 50 err := requete(apiserver.UrlDocuments, http.MethodDelete, IdAsParams(id), &out) 51 if err != nil { 52 c.ShowError(err) 53 return 54 } 55 c.Base.CleanupDocuments(rd.Ids{id}) 56 for k, v := range out { 57 c.Base.Contraintes[k] = v 58 } 59 c.ShowStandard("Document supprimé avec succès.", false) 60 } 61 62 func (c *MainController) loadCheck(docPath string) *os.File { 63 info, err := os.Stat(docPath) 64 if err != nil { 65 c.ShowError(err) 66 return nil 67 } 68 if info.Size() > apiserver.DOCUMENT_MAX_SIZE { 69 c.ShowError(fmt.Errorf("Le document est trop lourd : taille limite de <i>%d MB</i>, taille du document <b>%d KB</b> !", 70 apiserver.DOCUMENT_MAX_SIZE/1000000, info.Size()/1000)) 71 return nil 72 } 73 f, err := os.Open(docPath) 74 if err != nil { 75 c.ShowError(err) 76 return nil 77 } 78 return f 79 } 80 81 // uploadDocument upload le contenu sur le serveur et renvoi le document mis à jour. 82 func uploadDocument(idDoc int64, filename string, file *os.File, 83 monitor func(uint8)) (rd.Document, error) { 84 params := apiserver.ParamsUploadDocument{ 85 Filename: filename, 86 Content: file, 87 Id: idDoc, 88 } 89 var out rd.Document 90 err := requeteUpload(params, monitor, &out) 91 if err != nil { 92 return out, err 93 } 94 return out, nil 95 } 96 97 func (c *MainController) UploadDocument(docPath string, idDoc int64, monitor func(uint8)) *rd.Document { 98 file := c.loadCheck(docPath) 99 if file == nil { 100 return nil 101 } 102 c.ShowStandard("Téléversement vers le serveur...", true) 103 out, err := uploadDocument(idDoc, filepath.Base(docPath), file, monitor) 104 if err != nil { 105 c.ShowError(err) 106 return nil 107 } 108 c.Base.Documents[out.Id] = out 109 c.ShowStandard("Document téléversé avec succès", false) 110 return &out 111 } 112 113 func (c *MainController) CreeUploadDocument(docPath string, doc rd.Document, target rd.TargetDocument, monitor func(uint8)) { 114 file := c.loadCheck(docPath) 115 if file == nil { 116 return 117 } 118 c.ShowStandard("Téléversement du document...", true) 119 meta := apiserver.CreateDocumentIn{Document: doc, Target: target} 120 out, err := requeteCreateDocument(ParamsNewDocument{ 121 fileUpload: fileUpload{name: filepath.Base(docPath), content: file}, 122 Meta: meta, 123 }, monitor) 124 if err != nil { 125 c.ShowError(err) 126 return 127 } 128 out.Target.UpdateLinks(&c.Base.Links) 129 c.Base.Documents[out.Document.Id] = out.Document 130 c.ShowStandard("Document téléversé avec succès", false) 131 } 132 133 // DownloadDocument télécharge un document depuis la base de données, 134 // l'enregistre dans le dossier "local" et renvoie le chemin. 135 func (c *MainController) DownloadDocument(idDoc int64, monitor func(uint8)) string { 136 doc := c.Base.Documents[idDoc] 137 138 if monitor == nil { 139 monitor = func(m uint8) {} 140 } 141 docPath, err := filepath.Abs(filepath.Join(LocalFolder, string(doc.NomClient))) 142 if err != nil { 143 c.ShowError(err) 144 return "" 145 } 146 f, err := os.Create(docPath) 147 if err != nil { 148 c.ShowError(err) 149 return "" 150 } 151 err = requestDownload(apiserver.UrlDocumentsTransfert, http.MethodGet, IdAsParams(idDoc), monitor, f) 152 if err != nil { 153 c.ShowError(err) 154 return "" 155 } 156 if err := f.Close(); err != nil { 157 c.ShowError(err) 158 return "" 159 } 160 c.ShowStandard("Document téléchargé avec succès dans "+docPath, false) 161 return docPath 162 } 163 164 // SaveFile enregistre un document dans le dossier "local" et renvoie le chemin. 165 // S'occupe de gérer une potentielle erreur 166 func (c *MainController) SaveFile(doc []byte, filename string) string { 167 docPath, err := filepath.Abs(filepath.Join(LocalFolder, filename)) 168 if err != nil { 169 c.ShowError(err) 170 return "" 171 } 172 err = ioutil.WriteFile(docPath, doc, 0666) 173 if err != nil { 174 c.ShowError(err) 175 return "" 176 } 177 return docPath 178 }