mirror of
				https://github.com/antos-rde/antos-backend.git
				synced 2025-10-31 10:15:35 +01:00 
			
		
		
		
	store package cache in temporal file if unable to create cached file
	
		
			
	
		
	
	
		
	
		
			All checks were successful
		
		
	
	
		
			
				
	
				gitea-sync/antos-backend/pipeline/head This commit looks good
				
			
		
		
	
	
				
					
				
			
		
			All checks were successful
		
		
	
	gitea-sync/antos-backend/pipeline/head This commit looks good
				
			This commit is contained in:
		| @@ -1,25 +1,32 @@ | |||||||
| local packages={} | local packages = {} | ||||||
| local vfs = require("vfs") | local vfs = require("vfs") | ||||||
| local uid = ulib.uid(SESSION.user) | local uid = ulib.uid(SESSION.user) | ||||||
|  |  | ||||||
| packages._cache = function(y) | packages._cache = function(y) | ||||||
|     local p = vfs.ospath(y) |     local p = vfs.ospath(y) | ||||||
| 	local f = io.open(p.."/packages.json", "w") | 	local file_path = p .. "/packages.json" | ||||||
|  |     local f = io.open(file_path, "w") | ||||||
|     local has_cache = false |     local has_cache = false | ||||||
|     local i = 1 |     local i = 1 | ||||||
|     local meta = {} |     local meta = {} | ||||||
|  | 	 | ||||||
|  |     if not f then | ||||||
|  | 		-- store it is temporal file | ||||||
|  | 		file_path = string.format("%s/%s.packages.json",__api__.tmpdir, enc.sha1(p)) | ||||||
|  |         f = io.open(file_path, "w") | ||||||
|  |     end | ||||||
|     if f then |     if f then | ||||||
|         local files = vfs.readDir(y) |         local files = vfs.readDir(y) | ||||||
| 		for k,v in pairs(files) do |         for k, v in pairs(files) do | ||||||
|             if v.type == "dir" then |             if v.type == "dir" then | ||||||
| 				local f1 = io.open(vfs.ospath(v.path.."/package.json")) |                 local f1 = io.open(vfs.ospath(v.path .. "/package.json")) | ||||||
|                 if f1 then |                 if f1 then | ||||||
|  |  | ||||||
|                     local name = utils.basename(v.path) |                     local name = utils.basename(v.path) | ||||||
|                     local mt = JSON.decodeString(f1:read("*all")) |                     local mt = JSON.decodeString(f1:read("*all")) | ||||||
|                     mt.path = v.path |                     mt.path = v.path | ||||||
| 					meta[i] ='"'..name..'":'..JSON.encode(mt) |                     meta[i] = '"' .. name .. '":' .. JSON.encode(mt) | ||||||
| 					i = i+1 |                     i = i + 1 | ||||||
|                     f1:close() |                     f1:close() | ||||||
|                     has_cache = true; |                     has_cache = true; | ||||||
|                 end |                 end | ||||||
| @@ -28,7 +35,7 @@ packages._cache = function(y) | |||||||
|         f:write(table.concat(meta, ",")) |         f:write(table.concat(meta, ",")) | ||||||
|         f:close() |         f:close() | ||||||
|         if has_cache == false then |         if has_cache == false then | ||||||
| 			ulib.delete(p.."/packages.json"); |             ulib.delete(file_path); | ||||||
|         end |         end | ||||||
|     end |     end | ||||||
| end | end | ||||||
| @@ -38,12 +45,18 @@ packages.list = function(paths) | |||||||
|     std.json() |     std.json() | ||||||
|     std.t("{\"result\" : { ") |     std.t("{\"result\" : { ") | ||||||
|     local first = true |     local first = true | ||||||
| 	--std.f(__ROOT__.."/system/packages.json") |     for k, v in pairs(paths) do | ||||||
| 	for k,v in pairs(paths) do | 		local p = vfs.ospath(v) | ||||||
| 		local osp = vfs.ospath(v.."/packages.json") | 		local f1 = p.."/packages.json" | ||||||
| 		if  ulib.exists(osp) == false then | 		local f2 = string.format("%s/%s.packages.json",__api__.tmpdir, enc.sha1(p)) | ||||||
|  |  | ||||||
|  |         if not ulib.exists(f1) and not ulib.exists(f2) then | ||||||
|             packages._cache(v) |             packages._cache(v) | ||||||
|         end |         end | ||||||
|  | 		local osp = f1 | ||||||
|  | 		if not ulib.exists(osp) then | ||||||
|  | 			osp = f2 | ||||||
|  | 		end | ||||||
|         if ulib.exists(osp) then |         if ulib.exists(osp) then | ||||||
|             if first == false then |             if first == false then | ||||||
|                 std.t(",") |                 std.t(",") | ||||||
| @@ -59,7 +72,7 @@ end | |||||||
| -- generate the packages caches | -- generate the packages caches | ||||||
| packages.cache = function(args) | packages.cache = function(args) | ||||||
|     -- perform a packages caches |     -- perform a packages caches | ||||||
| 	for x,y in pairs(args.paths) do |     for x, y in pairs(args.paths) do | ||||||
|         packages._cache(y) |         packages._cache(y) | ||||||
|     end |     end | ||||||
|     result(true) |     result(true) | ||||||
| @@ -68,19 +81,19 @@ end | |||||||
| packages.install = function(args) | packages.install = function(args) | ||||||
|     local path = vfs.ospath(args.dest) |     local path = vfs.ospath(args.dest) | ||||||
|     local zip = vfs.ospath(args.zip) |     local zip = vfs.ospath(args.zip) | ||||||
| 	if(ulib.exists(path) == false) then |     if (ulib.exists(path) == false) then | ||||||
|         -- create directory if not exist |         -- create directory if not exist | ||||||
|         ulib.mkdir(path) |         ulib.mkdir(path) | ||||||
|         -- change permission |         -- change permission | ||||||
|         ulib.chown(path, uid.id, uid.gid) |         ulib.chown(path, uid.id, uid.gid) | ||||||
|     end |     end | ||||||
|     -- extract the zip file to it |     -- extract the zip file to it | ||||||
| 	if(ulib.unzip(zip, path)) then |     if (ulib.unzip(zip, path)) then | ||||||
|         -- read metadata |         -- read metadata | ||||||
| 		local meta = JSON.decodeFile(path.."/metadata.json") |         local meta = JSON.decodeFile(path .. "/metadata.json") | ||||||
|         meta.path = args.dest |         meta.path = args.dest | ||||||
|         meta.scope = "user" |         meta.scope = "user" | ||||||
| 		local f=io.open(path.."/package.json","w") |         local f = io.open(path .. "/package.json", "w") | ||||||
|         if f then |         if f then | ||||||
|             f:write(JSON.encode(meta)) |             f:write(JSON.encode(meta)) | ||||||
|             f:close() |             f:close() | ||||||
| @@ -94,8 +107,8 @@ end | |||||||
| -- uninstall the package | -- uninstall the package | ||||||
| packages.uninstall = function(path) | packages.uninstall = function(path) | ||||||
|     local osf = vfs.ospath(path) |     local osf = vfs.ospath(path) | ||||||
| 	if(osf and ulib.exists(osf) ) then |     if (osf and ulib.exists(osf)) then | ||||||
| 		--remove it |         -- remove it | ||||||
|         ulib.delete(osf) |         ulib.delete(osf) | ||||||
|         result(true) |         result(true) | ||||||
|     else |     else | ||||||
| @@ -104,8 +117,8 @@ packages.uninstall = function(path) | |||||||
| end | end | ||||||
| -- set user packages environment | -- set user packages environment | ||||||
| packages.init = function(paths) | packages.init = function(paths) | ||||||
| 	if(paths) then |     if (paths) then | ||||||
| 		for k,v in pairs(paths) do |         for k, v in pairs(paths) do | ||||||
|             local p = vfs.ospath(v) |             local p = vfs.ospath(v) | ||||||
|             if p and (ulib.exists(p) == false) then |             if p and (ulib.exists(p) == false) then | ||||||
|                 ulib.mkdir(p) |                 ulib.mkdir(p) | ||||||
|   | |||||||
							
								
								
									
										13
									
								
								libs/vfs.lua
									
									
									
									
									
								
							
							
						
						
									
										13
									
								
								libs/vfs.lua
									
									
									
									
									
								
							| @@ -3,19 +3,24 @@ local vfs = {} | |||||||
| vfs.ospath = function(path) | vfs.ospath = function(path) | ||||||
|     local user = SESSION.user |     local user = SESSION.user | ||||||
|     local prefix = string.match(path, "%a+:/") |     local prefix = string.match(path, "%a+:/") | ||||||
|  |     local os_path = nil | ||||||
|     if (prefix ~= nil) then |     if (prefix ~= nil) then | ||||||
|         local suffix = string.gsub(path, prefix, "") |         local suffix = string.gsub(path, prefix, "") | ||||||
|         if prefix == "home:/" then |         if prefix == "home:/" then | ||||||
|             return string.format(VFS_HOME, user) .. '/' .. suffix |             os_path = string.format(VFS_HOME, user) .. '/' .. suffix | ||||||
|         elseif prefix == "desktop:/" then |         elseif prefix == "desktop:/" then | ||||||
|             return string.format(VFS_HOME, user) .. "/.desktop/" .. suffix |             os_path = string.format(VFS_HOME, user) .. "/.desktop/" .. suffix | ||||||
|         elseif prefix == "shared:/" then |         elseif prefix == "shared:/" then | ||||||
|             return require("shared").ospath(ulib.trim(suffix, "/")) |             os_path = require("shared").ospath(ulib.trim(suffix, "/")) | ||||||
|         elseif prefix == "os:/" then |         elseif prefix == "os:/" then | ||||||
|             return WWW_ROOT .. "/" .. suffix |             os_path = WWW_ROOT .. "/" .. suffix | ||||||
|         else |         else | ||||||
|             return nil |             return nil | ||||||
|         end |         end | ||||||
|  |         while os_path:match("//") do | ||||||
|  |             os_path = os_path:gsub("//","/") | ||||||
|  |         end | ||||||
|  |         return os_path | ||||||
|     else |     else | ||||||
|         return nil; |         return nil; | ||||||
|     end |     end | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user