comparison mod_migrate_http_upload/mod_migrate_http_upload.lua @ 4468:5d8f9cc5c6fb

mod_migrate_http_upload: Upload data converter to mod_http_file_share
author Kim Alvefur <zash@zash.se>
date Wed, 24 Feb 2021 16:55:46 +0100
parents
children 203f0f06d766
comparison
equal deleted inserted replaced
4467:6d595857164a 4468:5d8f9cc5c6fb
1 -- Copyright (C) 2021 Kim Alvefur
2 --
3 -- This file is MIT licensed.
4
5 local lfs = require "lfs";
6 local st = require "util.stanza";
7 local jid = require "util.jid";
8 local paths = require "util.paths";
9 local unpack = table.unpack or _G.unpack;
10
11 function module.command(arg)
12 local sm = require "core.storagemanager";
13 local dm = sm.olddm;
14
15 local component, user_host = unpack(arg);
16
17 sm.initialize_host(component);
18
19 local new_uploads = sm.open(component, "uploads", "archive");
20
21 local legacy_uploads = {};
22 for user in assert(dm.users(user_host, "http_upload", "list")) do
23 legacy_uploads[user] = dm.list_load(user, user_host, "http_upload");
24 end
25 while true do
26 local oldest_uploads, uploader;
27 for user, uploads in pairs(legacy_uploads) do
28 if uploads[1] and (not oldest_uploads or uploads[1].time < oldest_uploads[1].time) then
29 oldest_uploads, uploader = uploads, jid.join(user, user_host);
30 end
31 end
32 if not oldest_uploads then break end
33 local item = table.remove(oldest_uploads, 1);
34 local source_directory = paths.join(prosody.paths.data, "http_upload", item.dir);
35 local source_filename = paths.join(prosody.paths.data, "http_upload", item.dir, item.filename);
36 local target_filename = dm.getpath(item.dir, component, "http_file_share", "bin", true);
37 if not lfs.attributes(source_filename, "mode") then
38 print("Not migrating missing file " .. source_filename);
39 else
40 print("Moving " .. source_filename .. " to " .. target_filename .. " for " .. uploader);
41 local upload = st.stanza("request", {
42 xmlns = "urn:xmpp:http:upload:0";
43 filename = item.filename;
44 size = string.format("%d", item.size);
45 -- content-type not included with mod_http_upload
46 });
47 assert(new_uploads:append(nil, item.dir, upload, item.time, uploader));
48 assert(os.rename(source_filename, target_filename));
49 end
50 os.remove(source_directory); -- failure not fatal
51 end
52 for user, uploads in pairs(legacy_uploads) do
53 assert(dm.list_store(user, user_host, "http_upload", uploads));
54 end
55 return 0;
56 end