Commit ac96f906 authored by Georges Racinet's avatar Georges Racinet 🦑
Browse files

Committing from Rails: work with UploadedFile content

This happens for instance with the "Replace" button of the
file web pages.

--HG--
branch : heptapod
parent b1da379ff1fc
......@@ -591,6 +591,25 @@ def pull_force_topic(url, hgsha, topic)
node_branch.split(':')
end
def write_file(abspath, content)
if content.is_a?(UploadedFile)
# TODO mv would be more efficient, but it's unclear how much the
# UploadedFile would be then broken and whether it matters.
# (probably not much, the tempfile is an open file anyway).
# Another option if on same FS (case where mv is efficient)
# would be to make a hardlink. Playing safe for now.
FileUtils.cp(content.path, abspath)
else
# File.write uses text mode, which forces conversion to
# Encoding.external_encoding ('UTF-8') and gives an error.
# On the other hand, a file explicitely open in binary mode
# is happy to receive an UTF-8 string
File.open(abspath, "wb") do |f|
f.write(content)
end
end
end
# perform multiple write actions in the repository in a single commit
#
# typically multiple file writes or renames, followed by a commit
......@@ -680,18 +699,12 @@ def multi_action(user, branch_name:, message:, actions:,
"already exists."
end
FileUtils.mkdir_p(parent_abspath)
# File.write uses text mode, which forces conversion to
# Encoding.external_encoding ('UTF-8') and gives an error.
# On the other hand, a file explicitely open in binary mode
# is happy to receive an UTF-8 string
File.open(abspath, "wb") do |f|
f.write(action[:content])
end
write_file(abspath, action[:content])
when :update
unless File.file?(abspath)
raise ActionError, "A file with this name doesn't exist"
end
File.write(abspath, action[:content])
write_file(abspath, action[:content])
when :create_dir
if File.file?(abspath)
raise ActionError, "A file with this name already exists "
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment