11import back_schemas from "@geode/opengeodeweb-back/opengeodeweb_back_schemas.json"
22import viewer_schemas from "@geode/opengeodeweb-viewer/opengeodeweb_viewer_schemas.json"
3+ import { useAppStore } from "../stores/app.js"
4+ import { useGeodeStore } from "../stores/geode.js"
5+ import { useInfraStore } from "../stores/infra.js"
6+ import { viewer_call } from "./viewer_call.js"
7+ import { api_fetch } from "./api_fetch.js"
38
49export function useProjectManager ( ) {
510 const appStore = useAppStore ( )
@@ -13,6 +18,7 @@ export function useProjectManager() {
1318 const schema = back_schemas . opengeodeweb_back . export_project
1419 const defaultName = "project.zip"
1520
21+ // Envoi systématique du filename pour éviter le 400 initial
1622 await api_fetch (
1723 { schema, params : { snapshot, filename : defaultName } } ,
1824 {
@@ -43,19 +49,72 @@ export function useProjectManager() {
4349 async function importProjectFile ( file ) {
4450 geode . start_request ( )
4551 try {
46- const snapshot = JSON . parse ( await file . text ( ) )
47- await useInfraStore ( ) . create_connection ( )
52+ const infra = useInfraStore ( )
53+ await infra . create_connection ( )
54+
55+ // ping backend sans useFetch
56+ const pingId = back_schemas . opengeodeweb_back . ping ?. $id || "opengeodeweb_back/ping"
57+ const pingURL = new URL ( "/" + String ( pingId ) , infra ?. base_url || window . location . origin ) . toString ( )
58+ await $fetch ( pingURL , { method : "POST" , body : { } } )
59+
60+ const isJson = ( file . name || "" ) . toLowerCase ( ) . endsWith ( ".json" )
61+ if ( isJson ) {
62+ const snapshot = JSON . parse ( await file . text ( ) )
63+ await viewer_call ( { schema : viewer_schemas . opengeodeweb_viewer . import_project , params : { } } )
64+ await viewer_call ( { schema : viewer_schemas . opengeodeweb_viewer . viewer . reset_visualization , params : { } } )
65+ await appStore . importStores ( snapshot )
66+ return
67+ }
68+
69+ // Import ZIP → multipart/form-data
70+ const form = new FormData ( )
71+ form . append ( "file" , file , file . name || "project.zip" )
72+
73+ const importId = back_schemas . opengeodeweb_back . import_project ?. $id || "opengeodeweb_back/import_project"
74+ const importURL = new URL ( "/" + String ( importId ) , infra ?. base_url || window . location . origin ) . toString ( )
75+
76+ let result
77+ try {
78+ result = await $fetch ( importURL , { method : "POST" , body : form } )
79+ } catch ( error ) {
80+ const status = error ?. response ?. status ?? error ?. status
81+ const data = error ?. response ?. _data ?? error ?. data
82+ if ( status === 423 ) {
83+ await viewer_call ( { schema : viewer_schemas . opengeodeweb_viewer . viewer . reset_visualization , params : { } } )
84+ result = await $fetch ( importURL , { method : "POST" , body : form } )
85+ } else {
86+ console . error ( "Backend import_project erreur:" , data ?? error )
87+ }
88+ }
89+
90+ // Si le backend renvoie un snapshot, on met à jour les stores
91+ if ( result ?. snapshot ) {
92+ await appStore . importStores ( result . snapshot )
93+ }
4894
49- await viewer_call ( {
50- schema : viewer_schemas . opengeodeweb_viewer . import_project ,
51- params : { } ,
52- } )
53- await viewer_call ( {
54- schema : viewer_schemas . opengeodeweb_viewer . reset_visualization ,
55- params : { } ,
56- } )
95+ // Si pas de viewables prêts, on tente des conversions courantes
96+ const needsViewables = result ?. viewables_ready === false || result == null
97+ if ( needsViewables ) {
98+ const candidates = [
99+ { input_geode_object : "BRep" , filename : "native/main.og_brep" } ,
100+ { input_geode_object : "SurfaceMesh" , filename : "native/main.og_mesh" } ,
101+ ]
102+ for ( const c of candidates ) {
103+ try {
104+ await api_fetch ( {
105+ schema : back_schemas . opengeodeweb_back . save_viewable_file ,
106+ params : { input_geode_object : c . input_geode_object , filename : c . filename } ,
107+ } )
108+ break
109+ } catch ( _ ) {
110+ // On ignore et on essaie le candidat suivant
111+ }
112+ }
113+ }
57114
58- await appStore . importStores ( snapshot )
115+ // Synchronisation Viewer
116+ await viewer_call ( { schema : viewer_schemas . opengeodeweb_viewer . import_project , params : { } } )
117+ await viewer_call ( { schema : viewer_schemas . opengeodeweb_viewer . viewer . reset_visualization , params : { } } )
59118 } finally {
60119 geode . stop_request ( )
61120 }
0 commit comments