Annotate

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