mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-03-04 19:53:19 +08:00
Implement restore functionality in file versions modal [SCI-11042]
This commit is contained in:
parent
f1eea587de
commit
48fb68c07c
10 changed files with 54 additions and 8 deletions
|
@ -197,7 +197,7 @@ class AssetsController < ApplicationController
|
|||
return render_403 unless can_read_team?(@asset.team)
|
||||
|
||||
@asset.last_modified_by = current_user
|
||||
@asset.attach_file_version(io: params.require(:image), filename: orig_file_name, current_user: current_user)
|
||||
@asset.attach_file_version(io: params.require(:image), filename: orig_file_name)
|
||||
@asset.save!
|
||||
create_edit_image_activity(@asset, current_user, :finish_editing)
|
||||
# release previous image space
|
||||
|
@ -405,6 +405,11 @@ class AssetsController < ApplicationController
|
|||
)
|
||||
end
|
||||
|
||||
def restore_version
|
||||
@asset.restore_file_version(params[:version].to_i)
|
||||
render json: @asset.file.blob
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def load_vars
|
||||
|
|
|
@ -27,6 +27,7 @@
|
|||
@attachment:changed="$emit('attachment:changed', $event)"
|
||||
@attachment:update="$emit('attachment:update', $event)"
|
||||
@menu-toggle="$emit('attachment:toggle_menu', $event)"
|
||||
@attachment:versionRestored="$emit('attachment:versionRestored', $event)"
|
||||
:withBorder="withBorder"
|
||||
/>
|
||||
</div>
|
||||
|
|
|
@ -58,8 +58,10 @@
|
|||
/>
|
||||
<FileVersionsModal
|
||||
v-if="fileVersionsModal"
|
||||
:url="attachment.attributes.urls.versions"
|
||||
:versionsUrl="attachment.attributes.urls.versions"
|
||||
:restoreVersionUrl="attachment.attributes.urls.restore_version"
|
||||
@close="fileVersionsModal = false"
|
||||
@fileVersionRestored="$emit('attachment:versionRestored', $event)"
|
||||
/>
|
||||
</Teleport>
|
||||
</div>
|
||||
|
|
|
@ -38,6 +38,7 @@
|
|||
@attachment:delete="deleteAttachment"
|
||||
@attachment:moved="attachmentMoved"
|
||||
@attachment:uploaded="reloadAttachments"
|
||||
@attachment:versionRestored="reloadAttachments"
|
||||
@attachment:changed="$emit('attachment:changed', $event)"
|
||||
@attachment:update="$emit('attachment:update', $event)"
|
||||
@attachment:toggle_menu="toggleMenuDropdown"
|
||||
|
|
|
@ -37,6 +37,7 @@
|
|||
@attachment:delete="deleteAttachment"
|
||||
@attachment:moved="attachmentMoved"
|
||||
@attachment:uploaded="reloadAttachments"
|
||||
@attachment:versionRestored="reloadAttachments"
|
||||
@attachment:changed="$emit('attachment:changed', $event)"
|
||||
@attachment:update="$emit('attachment:update', $event)"
|
||||
@attachment:toggle_menu="toggleMenuDropdown"
|
||||
|
|
|
@ -54,6 +54,7 @@
|
|||
@attachment:delete="deleteAttachment"
|
||||
@attachment:moved="attachmentMoved"
|
||||
@attachment:uploaded="reloadAttachments"
|
||||
@attachment:versionRestored="reloadAttachments"
|
||||
@attachment:changed="$emit('attachment:changed', $event)"
|
||||
@attachment:update="$emit('attachment:update', $event)"
|
||||
@attachment:toggle_menu="toggleMenu"
|
||||
|
|
|
@ -28,7 +28,7 @@
|
|||
|
||||
<div class="basis-1/4 flex justify-end">
|
||||
<a class="btn btn-icon p-0" :href="fileVersion.attributes.url" target="_blank"><i class="sn-icon sn-icon-export"></i></a>
|
||||
<a class="btn btn-icon p-0 mx-3"><i class="sn-icon sn-icon-restore"></i></a>
|
||||
<a v-if="restoreVersionUrl" @click="restoreVersion(fileVersion.attributes.version)" class="btn btn-icon p-0 mx-3"><i class="sn-icon sn-icon-restore"></i></a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
@ -46,7 +46,11 @@ import axios from '../../packs/custom_axios';
|
|||
export default {
|
||||
name: 'FileVersionsModal',
|
||||
props: {
|
||||
url: {
|
||||
versionsUrl: {
|
||||
type: String,
|
||||
required: true
|
||||
},
|
||||
restoreVersionUrl: {
|
||||
type: String,
|
||||
required: true
|
||||
}
|
||||
|
@ -58,9 +62,20 @@ export default {
|
|||
};
|
||||
},
|
||||
created() {
|
||||
axios.get(this.url).then((response) => {
|
||||
this.fileVersions = response.data.data;
|
||||
});
|
||||
this.loadVersions();
|
||||
},
|
||||
methods: {
|
||||
loadVersions() {
|
||||
axios.get(this.versionsUrl).then((response) => {
|
||||
this.fileVersions = response.data.data;
|
||||
});
|
||||
},
|
||||
restoreVersion(version) {
|
||||
axios.post(this.restoreVersionUrl, { version: version }).then(() => {
|
||||
this.loadVersions();
|
||||
this.$emit('fileVersionRestored');
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
</script>
|
||||
|
|
|
@ -26,6 +26,24 @@ module VersionedAttachments
|
|||
new_blob.save!
|
||||
end
|
||||
end
|
||||
|
||||
define_method :"restore_#{name}_version" do |version|
|
||||
ActiveRecord::Base.transaction(requires_new: true) do
|
||||
blob = __send__(:"previous_#{name.to_s.pluralize}").map(&:blob).find do |b|
|
||||
(b.metadata['version'] || 1) == version
|
||||
end
|
||||
|
||||
blob.open do |tmp_file|
|
||||
new_blob = ActiveStorage::Blob.create_and_upload!(
|
||||
io: tmp_file,
|
||||
filename: blob.filename,
|
||||
metadata: blob.metadata.merge({ 'restored_from_version' => version })
|
||||
)
|
||||
|
||||
__send__(:"attach_#{name}_version", new_blob)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -152,7 +152,8 @@ class AssetSerializer < ActiveModel::Serializer
|
|||
duplicate: asset_duplicate_path(object),
|
||||
move_targets: asset_move_tagets_path(object),
|
||||
move: asset_move_path(object),
|
||||
rename: asset_rename_path(object)
|
||||
rename: asset_rename_path(object),
|
||||
restore_version: asset_restore_version_path(object)
|
||||
)
|
||||
end
|
||||
|
||||
|
|
|
@ -862,6 +862,7 @@ Rails.application.routes.draw do
|
|||
get 'files/:id/checksum', to: 'assets#checksum', as: 'asset_checksum'
|
||||
get 'files/:id/show', to: 'assets#show', as: 'asset_show'
|
||||
get 'files/:id/versions', to: 'assets#versions', as: 'asset_versions'
|
||||
post 'files/:id/restore_version', to: 'assets#restore_version', as: 'asset_restore_version'
|
||||
patch 'files/:id/toggle_view_mode', to: 'assets#toggle_view_mode', as: 'toggle_view_mode'
|
||||
get 'files/:id/load_asset', to: 'assets#load_asset', as: 'load_asset'
|
||||
post 'files/:id/update_image', to: 'assets#update_image',
|
||||
|
|
Loading…
Reference in a new issue