changeset 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 6d595857164a
children 6844733ad2f5
files mod_migrate_http_upload/README.markdown mod_migrate_http_upload/mod_migrate_http_upload.lua
diffstat 2 files changed, 87 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mod_migrate_http_upload/README.markdown	Wed Feb 24 16:55:46 2021 +0100
@@ -0,0 +1,31 @@
+---
+summary: mod_http_upload to mod_http_file_share migrator
+labels:
+- Stage-Alpha
+---
+
+
+This is a migration script for converting records of [mod_http_upload]
+into the format used by the new [mod_http_file_share] which will be
+available with Prosody 0.12 (currently in trunk).
+
+# Usage
+
+If your main `VirtualHost` is called "example.com" and your HTTP upload
+`Component` is called "upload.example.com", then this command would
+convert records of existing uploads via [mod_http_upload] to
+[mod_http_file_share][doc:modules:mod_http_file_share]:
+
+```bash
+sudo prosodyctl mod_migrate_http_upload upload.example.com example.com
+```
+
+In order to preserve URLs you will need to configure the
+[path][doc:http#path_configuration] to be the same as mod_http_upload:
+
+```lua
+Component "upload.example.com" "http_file_share"
+http_paths = {
+    file_share = "/upload"
+}
+```
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mod_migrate_http_upload/mod_migrate_http_upload.lua	Wed Feb 24 16:55:46 2021 +0100
@@ -0,0 +1,56 @@
+-- Copyright (C) 2021 Kim Alvefur
+--
+-- This file is MIT licensed.
+
+local lfs = require "lfs";
+local st = require "util.stanza";
+local jid = require "util.jid";
+local paths = require "util.paths";
+local unpack = table.unpack or _G.unpack;
+
+function module.command(arg)
+	local sm = require "core.storagemanager";
+	local dm = sm.olddm;
+
+	local component, user_host = unpack(arg);
+
+	sm.initialize_host(component);
+
+	local new_uploads = sm.open(component, "uploads", "archive");
+
+	local legacy_uploads = {};
+	for user in assert(dm.users(user_host, "http_upload", "list")) do
+		legacy_uploads[user] = dm.list_load(user, user_host, "http_upload");
+	end
+	while true do
+		local oldest_uploads, uploader;
+		for user, uploads in pairs(legacy_uploads) do
+			if uploads[1] and (not oldest_uploads or uploads[1].time < oldest_uploads[1].time) then
+				oldest_uploads, uploader = uploads, jid.join(user, user_host);
+			end
+		end
+		if not oldest_uploads then break end
+		local item = table.remove(oldest_uploads, 1);
+		local source_directory = paths.join(prosody.paths.data, "http_upload", item.dir);
+		local source_filename = paths.join(prosody.paths.data, "http_upload", item.dir, item.filename);
+		local target_filename = dm.getpath(item.dir, component, "http_file_share", "bin", true);
+		if not lfs.attributes(source_filename, "mode") then
+			print("Not migrating missing file " .. source_filename);
+		else
+			print("Moving " .. source_filename .. " to " .. target_filename .. " for " .. uploader);
+			local upload = st.stanza("request", {
+				xmlns = "urn:xmpp:http:upload:0";
+				filename = item.filename;
+				size = string.format("%d", item.size);
+				-- content-type not included with mod_http_upload
+			});
+			assert(new_uploads:append(nil, item.dir, upload, item.time, uploader));
+			assert(os.rename(source_filename, target_filename));
+		end
+		os.remove(source_directory); -- failure not fatal
+	end
+	for user, uploads in pairs(legacy_uploads) do
+		assert(dm.list_store(user, user_host, "http_upload", uploads));
+	end
+	return 0;
+end