@@ -28,61 +28,68 @@ ClassMethod NamespaceCheck(pNamespace As %String) As %Boolean [ Internal, Privat
2828
2929ClassMethod PutMigrate () As %DynamicObject
3030{
31- // Get the request body
32- set dyna = {}.%FromJSON (%request .Content )
33- set body = dyna .%Get (" body" )
34- set namespace = dyna .%Get (" namespace" )
35- set targetDirectory = dyna .%Get (" remote_folder" )
36- set packageName = dyna .%Get (" package" )
37- // check for namespace existence and user permissions against namespace
38- If '..NamespaceCheck (namespace ) {
39- Return " "
40- }
41- New $NAMESPACE
42- Set $NAMESPACE = namespace
31+ Try {
32+ // Get the request body
33+ set dyna = {}.%FromJSON (%request .Content )
34+ set body = dyna .%Get (" body" )
35+ set namespace = dyna .%Get (" namespace" )
36+ set targetDirectory = dyna .%Get (" remote_folder" )
37+ set packageName = dyna .%Get (" package" )
38+ // check for namespace existence and user permissions against namespace
39+ If '..NamespaceCheck (namespace ) {
40+ Return " "
41+ }
42+ New $NAMESPACE
43+ Set $NAMESPACE = namespace
4344
44-
45- //Create directory for custom packages
46- If targetDirectory '= " " {
47- If '##class (%Library.File ).DirectoryExists (targetDirectory ) {
48- If '##class (%Library.File ).CreateDirectory (targetDirectory ) {
49- $$$ThrowStatus($$$ERROR($$$DirectoryCannotCreate, targetDirectory ))
50- }
51- }
52- }
53- Else {
54- // Set targetDirectory to the CODE DATABASE directory
55- do ##class (%SYS.Namespace ).GetAllNSInfo (" USER" ,.info )
56- Set targetDirectory = info (" RoutineDB" ," Directory" )
57- }
58-
59- Set packagePath = ##class (%Library.File ).NormalizeDirectory (packageName , targetDirectory )
60- // If the package already exists then we must be meaning to re-load it. Delete files/directory/metadata and recreate fresh.
61- If ##class (%Library.File ).DirectoryExists (packagePath ) {
62- If '##class (%Library.File ).RemoveDirectoryTree (packagePath ) {
63- $$$ThrowStatus($$$ERROR($$$DirectoryPermission , packagePath ))
45+
46+ //Create directory for custom packages
47+ If targetDirectory '= " " {
48+ If '##class (%Library.File ).DirectoryExists (targetDirectory ) {
49+ If '##class (%Library.File ).CreateDirectory (targetDirectory ) {
50+ $$$ThrowStatus($$$ERROR($$$DirectoryCannotCreate, targetDirectory ))
51+ }
52+ }
53+ }
54+ Else {
55+ // Set targetDirectory to the CODE DATABASE directory
56+ do ##class (%SYS.Namespace ).GetAllNSInfo (" USER" ,.info )
57+ Set targetDirectory = info (" RoutineDB" ," Directory" )
58+ }
59+
60+ Set packagePath = ##class (%Library.File ).NormalizeDirectory (packageName , targetDirectory )
61+ // If the package already exists then we must be meaning to re-load it. Delete files/directory/metadata and recreate fresh.
62+ If ##class (%Library.File ).DirectoryExists (packagePath ) {
63+ If '##class (%Library.File ).RemoveDirectoryTree (packagePath ) {
64+ $$$ThrowStatus($$$ERROR($$$DirectoryPermission , packagePath ))
65+ }
66+ }
67+ If '##class (%Library.File ).CreateDirectory (packagePath ) {
68+ $$$ThrowStatus($$$ERROR($$$DirectoryCannotCreate, packagePath ))
69+ }
70+
71+ //Unpack JSON objects
72+ Set iterator = body .%GetIterator ()
73+ While iterator .%GetNext (.key , .fileObject ) {
74+ // If fileObject.name has '/' then it is a path, we need to normalize it
75+ Set fileName = ##class (%Library.File ).NormalizeFilename (fileObject .name ,packagePath )
76+ do ##class (%Library.File ).CreateDirectoryChain (##class (%Library.File ).GetDirectory (fileName ))
77+ Set fileStream = ##class (%Stream.FileCharacter ).%New ()
78+ Set fileStream .TranslateTable = " UTF8"
79+ $$$ThrowOnError(fileStream .LinkToFile (fileName ))
80+ Do fileStream .Write (fileObject .data )
81+ $$$ThrowOnError(fileStream .%Save ())
6482 }
65- }
66- If '##class (%Library.File ).CreateDirectory (packagePath ) {
67- $$$ThrowStatus($$$ERROR($$$DirectoryCannotCreate, packagePath ))
68- }
6983
70- //Unpack JSON objects
71- Set iterator = body .%GetIterator ()
72- While iterator .%GetNext (.key , .fileObject ) {
73- // If fileObject.name has '/' then it is a path, we need to normalize it
74- Set fileName = ##class (%Library.File ).NormalizeFilename (fileObject .name ,packagePath )
75- do ##class (%Library.File ).CreateDirectoryChain (##class (%Library.File ).GetDirectory (fileName ))
76- Set fileStream = ##class (%Stream.FileCharacter ).%New ()
77- Set fileStream .TranslateTable = " UTF8"
78- $$$ThrowOnError(fileStream .LinkToFile (fileName ))
79- Do fileStream .Write (fileObject .data )
80- $$$ThrowOnError(fileStream .%Save ())
84+ //Do the iop migration
85+
86+ set iopUtils = ##class (IOP.Wrapper ).Import (" iop._utils" )
87+ do iopUtils ." _Utils" .migrate (##class (%Library.File ).NormalizeFilename (" settings.py" , packagePath ))
88+ }
89+ Catch ex {
90+ $$$ThrowOnError(ex .AsStatus ())
8191 }
8292
83- //Do the iop migration
84- set iopUtils = ##class (IOP.Wrapper ).Import (" iop._utils" )
85- do iopUtils ." _Utils" .migrate (##class (%Library.File ).NormalizeFilename (" settings.py" , packagePath ))
8693
8794 return $$$OK
8895}
0 commit comments