Raudius / files_scripts

Custom file actions app for Nextcloud
GNU Affero General Public License v3.0
25 stars 11 forks source link

Processing several files #170

Open ostasevych opened 1 month ago

ostasevych commented 1 month ago

Hi!

I have created the scripts to archive and unarchive files and folders. I've managed to achieve that for 1 selected item (file and/or folder), however cannot understand how to do that with more than 1 file or folder.

So, this is an example to archive file or folder:

-- Archive files with the help of 7z and rar
local input_file = get_input_files()[1]
local selected_files = #get_input_files()
local archiver = get_input("selected_archiver")
local password = get_input("archive_password")
local dir = get_input("output_location")
local supported_formats = { 'zip', '7z', 'rar', 'gzip', 'bzip', 'bzip2', 'tar' }
local input_file_extension = input_file.name:match("[^.]+$")

-- Function to check file presence of system apps
local function file_exists(name)
   local f=io.open(name,"r")
   if f~=nil then io.close(f) return true else return false end
end

-- Function to check whether a table contains some element
local function table_contains(table, element)
    for _, value in pairs(table) do
        -- We grab the first index of our sub-table instead
        if value == element:lower() then
            return true
        end
    end
    return false
end

local function table_not_contains(table, element)
    for _, value in pairs(table) do
        -- We grab the first index of our sub-table instead
        if value == element:lower() then
            return false
        end
    end
    return true
end

-- Function to search substring

local function get_lines_with_text(inputstr, search)
    local t={}
    for str in string.gmatch(inputstr, "[^\n]*"..search.."[^\n]*") do
        table.insert(t, str)
    end
    return t
end

-- Checking presence of system apps to convert files

if ((not file_exists('/usr/bin/7za')) and (not file_exists('/usr/local/bin/7za'))) then 
    abort("Потрібно встановити програму p7zip.")
end

if ((not file_exists('/usr/bin/rar')) and (not file_exists('/usr/local/bin/rar'))) then 
    abort("Потрібно встановити програму p7zip.")
end

-- Check if not selected more than 1 file
if (selected_files > 1) then abort("Вибрати для архівування поки що можна тільки один файл або каталог.") end

-- Defining default output format ZIP
if (archiver == nil or archiver == "") then
    archiver = "zip"
end

-- Creating current date stamp
now = create_date_time()
timezone = "EET"
format = "ddMMyyyy_HHmm"
current_date_stamp = format_date_time(now, nil, timezone, format)

-- Checking that the output location is defined
if (dir == nil or dir == "") then
   dir={}
   dir.path = input_file.path
   dir.name = ""
--    abort("Виберіть місце збереження конвертованого файлу");
end

-- Check if the selected item is folder or file
if (is_folder(input_file) == true) then
-- Defining that the output_name is the same as the input name but with archiver extension 
  output_name = input_file.name .. "." .. archiver
-- Defining output_name with current date stamp
  output_name_stamp = input_file.name .. "_" .. current_date_stamp .. "." .. archiver
  --  abort("Ви вибрали каталог, а не файл. Конвертування застосовується тільки до файлів!")
end

if (is_folder(input_file) == false) then
-- Defining that the output_name is the same as the input name but with archiver extension
  output_name = input_file.name:match("(.+)%..+") .. "." .. archiver
-- Defining output_name with current date stamp
  output_name_stamp = input_file.name:match("(.+)%..+") .. "_" .. current_date_stamp .. "." .. archiver
-- Checking that the destination file exists with the same extension, if yes, add date fingerprint
end

-- Add timestamp if the current file exists
if (exists(dir,output_name)) then 
--    abort('exists, adding ' .. output_name_stamp)
    output_name = output_name_stamp
end

-- Defining output_file
-- local output_file = new_file(dir, output_name)
-- local output_file = new_file(dir, output_name)

-- Adding full paths
local input_path = meta_data(input_file).local_path
local output_path = meta_data(dir).local_path .. '/' .. output_name

-- Processing files
if archiver ~= 'rar' then
  if (password == nil or password == "") then
    command_tpl = '7za -t%s a "%s" "%s"'
    command = command_tpl:format(archiver, output_path, input_path)
  else
    command_tpl = '7za -p%s -t%s a "%s" "%s"'
    command = command_tpl:format(password, archiver, output_path, input_path)
  end
else
  if (password == nil or password == "") then
    password = "-"
  end
  command_tpl = 'rar -p%s -ep a "%s" "%s"'
  command = command_tpl:format(password, output_path, input_path)
end

result = shell_command(command)

-- Handle error
if result.exit_code == 0 then
  add_message("Успішно створено архів. Оновіть сторінку!", "info")
else 
  abort("Помилка: " .. result.errors)
end

-- Postprocess to rescan folder with created file
local output_storage_path = meta_data(dir).storage_path
command2_tpl = 'php /usr/local/bin/occ files:scan --path="%s"'
command2 = command2_tpl:format(output_storage_path)
result = shell_command(command2)

-- Handle result
if result.exit_code ~= 0 then 
  add_message("Помилка: " .. result.errors,"error")
end

I will be thankful for your tips.

ostasevych commented 3 weeks ago

So, the main question, how can I select several files to be processed by the script? I am not so fluent with LUA, so, I will be thankful for the tips how to work with several selected files to pipe them to rar or 7za program via shell_command.