FIX: Moving upload to tombstone should update modification time.

A upload created a long time ago will be nuked from the tombstone
immediately if it gets deleted.
This commit is contained in:
Guo Xiang Tan 2018-09-18 10:48:29 +08:00
parent f2fbf1fdb0
commit ce6a0a5e9e
3 changed files with 49 additions and 16 deletions

View File

@ -17,6 +17,7 @@ module FileStore
dir = Pathname.new(destination).dirname
FileUtils.mkdir_p(dir) unless Dir.exists?(dir)
FileUtils.move(source, destination, force: true)
FileUtils.touch(destination)
end
def has_been_uploaded?(url)

View File

@ -10,7 +10,7 @@ describe FileStore::LocalStore do
let(:optimized_image) { Fabricate(:optimized_image) }
describe ".store_upload" do
describe "#store_upload" do
it "returns a relative url" do
store.expects(:copy_file)
@ -19,7 +19,7 @@ describe FileStore::LocalStore do
end
describe ".store_optimized_image" do
describe "#store_optimized_image" do
it "returns a relative url" do
store.expects(:copy_file)
@ -28,7 +28,7 @@ describe FileStore::LocalStore do
end
describe ".remove_upload" do
describe "#remove_upload" do
it "does not delete non uploaded" do
FileUtils.expects(:mkdir_p).never
@ -36,26 +36,58 @@ describe FileStore::LocalStore do
end
it "moves the file to the tombstone" do
filename = File.basename(store.path_for(upload))
store.remove_upload(upload)
expect(File.exist?(store.tombstone_dir + "/" + filename))
begin
upload = UploadCreator.new(
file_from_fixtures("smallest.png"),
"smallest.png"
).create_for(Fabricate(:user).id)
path = store.path_for(upload)
mtime = File.mtime(path)
sleep 0.01 # Delay a little for mtime to be updated
store.remove_upload(upload)
tombstone_path = path.sub("/uploads/", "/uploads/tombstone/")
expect(File.exist?(tombstone_path)).to eq(true)
expect(File.mtime(tombstone_path)).to_not eq(mtime)
ensure
[path, tombstone_path].each do |file_path|
File.delete(file_path) if File.exist?(file_path)
end
end
end
end
describe ".remove_optimized_image" do
let(:optimized_image) { Fabricate(:optimized_image, url: "/uploads/default/_optimized/42/253dc8edf9d4ada1.png") }
describe "#remove_optimized_image" do
it "moves the file to the tombstone" do
FileUtils.expects(:mkdir_p)
FileUtils.expects(:move)
File.expects(:exists?).returns(true)
store.remove_optimized_image(optimized_image)
begin
upload = UploadCreator.new(
file_from_fixtures("smallest.png"),
"smallest.png"
).create_for(Fabricate(:user).id)
upload.create_thumbnail!(1, 1)
upload.reload
optimized_image = upload.thumbnail(1, 1)
path = store.path_for(optimized_image)
store.remove_optimized_image(optimized_image)
tombstone_path = path.sub("/uploads/", "/uploads/tombstone/")
expect(File.exist?(tombstone_path)).to eq(true)
ensure
[path, tombstone_path].each do |file_path|
File.delete(file_path) if File.exist?(file_path)
end
end
end
end
describe ".has_been_uploaded?" do
describe "#has_been_uploaded?" do
it "identifies relatives urls" do
expect(store.has_been_uploaded?("/uploads/default/42/0123456789ABCDEF.jpg")).to eq(true)
@ -85,7 +117,7 @@ describe FileStore::LocalStore do
Discourse.stubs(:base_uri).returns("/forum")
end
describe ".absolute_base_url" do
describe "#absolute_base_url" do
it "is present" do
expect(store.absolute_base_url).to eq("http://test.localhost/uploads/default")
@ -98,7 +130,7 @@ describe FileStore::LocalStore do
end
describe ".relative_base_url" do
describe "#relative_base_url" do
it "is present" do
expect(store.relative_base_url).to eq("/uploads/default")

BIN
spec/fixtures/images/smallest.png vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 67 B