mirror of
https://github.com/VueFileManager/vuefilemanager.git
synced 2026-04-05 18:23:48 +00:00
user zipping
This commit is contained in:
@@ -338,7 +338,6 @@ return [
|
||||
'context_menu.share_cancel' => 'Cancel Sharing',
|
||||
'context_menu.share_edit' => 'Edit Sharing',
|
||||
'context_menu.upload' => 'Upload',
|
||||
'context_menu.zip_folder' => 'Zip and Download',
|
||||
'cookie_disclaimer.button' => 'cookies policy',
|
||||
'cookie_disclaimer.description' => 'By browsing this website you are agreeing to our {0}.',
|
||||
'datatable.paginate_info' => 'Showing 1 - {visible} from {total} records',
|
||||
|
||||
@@ -40,7 +40,7 @@
|
||||
"/chunks/files~chunks/platform~chunks/shared~chunks/shared/file-browser.js": "/chunks/files~chunks/platform~chunks/shared~chunks/shared/file-browser.js?id=8a06b7d864acff647f8c",
|
||||
"/chunks/files~chunks/platform~chunks/shared~chunks/shared/file-browser~chunks/shared/single-file.js": "/chunks/files~chunks/platform~chunks/shared~chunks/shared/file-browser~chunks/shared/single-file.js?id=0de0b81edf0bb5d4617d",
|
||||
"/chunks/files~chunks/settings-subscription~chunks/shared/file-browser~chunks/user-subscription.js": "/chunks/files~chunks/settings-subscription~chunks/shared/file-browser~chunks/user-subscription.js?id=c5ec9502bcfad35c502e",
|
||||
"/chunks/files~chunks/shared/file-browser.js": "/chunks/files~chunks/shared/file-browser.js?id=5c31a056cb557ade51c2",
|
||||
"/chunks/files~chunks/shared/file-browser.js": "/chunks/files~chunks/shared/file-browser.js?id=1b19035df0776555ab90",
|
||||
"/chunks/files~chunks/shared/file-browser~chunks/shared/single-file.js": "/chunks/files~chunks/shared/file-browser~chunks/shared/single-file.js?id=ad09e3f973e4db0411f1",
|
||||
"/chunks/forgotten-password.js": "/chunks/forgotten-password.js?id=8871529af0da8193d3a3",
|
||||
"/chunks/homepage.js": "/chunks/homepage.js?id=d29b9f09d08d673dff75",
|
||||
@@ -56,7 +56,7 @@
|
||||
"/chunks/plan-subscribers.js": "/chunks/plan-subscribers.js?id=320263fdc9aef3a3be60",
|
||||
"/chunks/plans.js": "/chunks/plans.js?id=0533e61243eeb87b3e8e",
|
||||
"/chunks/platform.js": "/chunks/platform.js?id=721cb528aaff1d69d58f",
|
||||
"/chunks/platform~chunks/shared.js": "/chunks/platform~chunks/shared.js?id=e5dcf772403613fce36c",
|
||||
"/chunks/platform~chunks/shared.js": "/chunks/platform~chunks/shared.js?id=f7ea7a882f169ecc2c31",
|
||||
"/chunks/profile.js": "/chunks/profile.js?id=7186e04a6f0f5b6bf470",
|
||||
"/chunks/profile~chunks/settings-password.js": "/chunks/profile~chunks/settings-password.js?id=ddb7be518c092ed392ca",
|
||||
"/chunks/purchase-code.js": "/chunks/purchase-code.js?id=85217c42d79948008ed1",
|
||||
@@ -107,5 +107,24 @@
|
||||
"/js/main.8b39d84cfbd9e8d53355.hot-update.js": "/js/main.8b39d84cfbd9e8d53355.hot-update.js",
|
||||
"/js/main.571d6eb6e4cdc38cf4e7.hot-update.js": "/js/main.571d6eb6e4cdc38cf4e7.hot-update.js",
|
||||
"/js/main.0a092bdc430e651e46a6.hot-update.js": "/js/main.0a092bdc430e651e46a6.hot-update.js",
|
||||
"/js/main.a2e4ef0944dba57924a8.hot-update.js": "/js/main.a2e4ef0944dba57924a8.hot-update.js"
|
||||
"/js/main.a2e4ef0944dba57924a8.hot-update.js": "/js/main.a2e4ef0944dba57924a8.hot-update.js",
|
||||
"/js/main.d52a3d051ffb259f0e6f.hot-update.js": "/js/main.d52a3d051ffb259f0e6f.hot-update.js",
|
||||
"/js/main.f694b1fcdad52320eae0.hot-update.js": "/js/main.f694b1fcdad52320eae0.hot-update.js",
|
||||
"/chunks/files~chunks/shared/file-browser.24beea145b487fbe999e.hot-update.js": "/chunks/files~chunks/shared/file-browser.24beea145b487fbe999e.hot-update.js",
|
||||
"/js/main.68f8c97da59bcec0f39e.hot-update.js": "/js/main.68f8c97da59bcec0f39e.hot-update.js",
|
||||
"/js/main.1b0b847042e83c0b0d7b.hot-update.js": "/js/main.1b0b847042e83c0b0d7b.hot-update.js",
|
||||
"/js/main.2354a675171466740151.hot-update.js": "/js/main.2354a675171466740151.hot-update.js",
|
||||
"/js/main.65431fc372485102df15.hot-update.js": "/js/main.65431fc372485102df15.hot-update.js",
|
||||
"/js/main.626dfb15dee5524edb97.hot-update.js": "/js/main.626dfb15dee5524edb97.hot-update.js",
|
||||
"/chunks/files~chunks/shared/file-browser.5047a001f7fdd11a038b.hot-update.js": "/chunks/files~chunks/shared/file-browser.5047a001f7fdd11a038b.hot-update.js",
|
||||
"/chunks/files~chunks/shared/file-browser.611a7944a1e9286325bc.hot-update.js": "/chunks/files~chunks/shared/file-browser.611a7944a1e9286325bc.hot-update.js",
|
||||
"/chunks/files~chunks/shared/file-browser.b343377ecc34e61e966d.hot-update.js": "/chunks/files~chunks/shared/file-browser.b343377ecc34e61e966d.hot-update.js",
|
||||
"/js/main.ba42843c1c7ac82ccafd.hot-update.js": "/js/main.ba42843c1c7ac82ccafd.hot-update.js",
|
||||
"/chunks/files~chunks/shared/file-browser.ba42843c1c7ac82ccafd.hot-update.js": "/chunks/files~chunks/shared/file-browser.ba42843c1c7ac82ccafd.hot-update.js",
|
||||
"/chunks/platform~chunks/shared.ba42843c1c7ac82ccafd.hot-update.js": "/chunks/platform~chunks/shared.ba42843c1c7ac82ccafd.hot-update.js",
|
||||
"/chunks/platform~chunks/shared.5afa70053cbd4fe99bef.hot-update.js": "/chunks/platform~chunks/shared.5afa70053cbd4fe99bef.hot-update.js",
|
||||
"/chunks/platform~chunks/shared.c0f59c4d5f95687a84ad.hot-update.js": "/chunks/platform~chunks/shared.c0f59c4d5f95687a84ad.hot-update.js",
|
||||
"/chunks/platform~chunks/shared.6db75d5fc148ae19b0b8.hot-update.js": "/chunks/platform~chunks/shared.6db75d5fc148ae19b0b8.hot-update.js",
|
||||
"/chunks/platform~chunks/shared.a9aa2769c5345e07f90c.hot-update.js": "/chunks/platform~chunks/shared.a9aa2769c5345e07f90c.hot-update.js",
|
||||
"/chunks/platform~chunks/shared.8a25cb105761d2e120e8.hot-update.js": "/chunks/platform~chunks/shared.8a25cb105761d2e120e8.hot-update.js"
|
||||
}
|
||||
|
||||
@@ -46,8 +46,7 @@
|
||||
|
||||
<OptionGroup v-if="item && isMultiSelectContextMenu">
|
||||
<Option @click.native="ItemDetail" :title="$t('context_menu.detail')" icon="detail" />
|
||||
<Option @click.native="downloadItem" v-if="!isFolder" :title="$t('context_menu.download')" icon="download" />
|
||||
<Option @click.native="downloadFolder" v-if="isFolder" :title="$t('context_menu.zip_folder')" icon="zip-folder" />
|
||||
<Option @click.native="downloadItem" :title="$t('context_menu.download')" icon="download" />
|
||||
</OptionGroup>
|
||||
|
||||
<!-- Multi options -->
|
||||
@@ -89,8 +88,7 @@
|
||||
|
||||
<OptionGroup v-if="item && isMultiSelectContextMenu ">
|
||||
<Option @click.native="ItemDetail" :title="$t('context_menu.detail')" icon="detail" />
|
||||
<Option @click.native="downloadItem" v-if="!isFolder" :title="$t('context_menu.download')" icon="download" />
|
||||
<Option @click.native="downloadFolder" v-if="isFolder" :title="$t('context_menu.zip_folder')" icon="zip-folder" />
|
||||
<Option @click.native="downloadItem" :title="$t('context_menu.download')" icon="download" />
|
||||
</OptionGroup>
|
||||
|
||||
<!-- Multi options -->
|
||||
@@ -103,7 +101,7 @@
|
||||
<Option @click.native="$deleteFileOrFolder(item)" :title="$t('context_menu.delete')" icon="trash" />
|
||||
</OptionGroup>
|
||||
|
||||
<OptionGroup v-if="item && !isMultiSelectContextMenu && !hasFolder">
|
||||
<OptionGroup v-if="item && !isMultiSelectContextMenu">
|
||||
<Option @click.native="downloadItem" :title="$t('context_menu.download')" icon="download" />
|
||||
</OptionGroup>
|
||||
</div>
|
||||
@@ -125,8 +123,7 @@
|
||||
|
||||
<OptionGroup v-if="item && isMultiSelectContextMenu">
|
||||
<Option @click.native="ItemDetail" :title="$t('context_menu.detail')" icon="detail" />
|
||||
<Option @click.native="downloadItem" v-if="!isFolder" :title="$t('context_menu.download')" icon="download" />
|
||||
<Option @click.native="downloadFolder" v-if="isFolder" :title="$t('context_menu.zip_folder')" icon="zip-folder" />
|
||||
<Option @click.native="downloadItem" :title="$t('context_menu.download')" icon="download" />
|
||||
</OptionGroup>
|
||||
|
||||
<!-- Multi options -->
|
||||
@@ -146,8 +143,7 @@
|
||||
<!-- Single options -->
|
||||
<OptionGroup v-if="item && isMultiSelectContextMenu">
|
||||
<Option @click.native="ItemDetail" :title="$t('context_menu.detail')" icon="detail" />
|
||||
<Option @click.native="downloadItem" v-if="!isFolder" :title="$t('context_menu.download')" icon="download" />
|
||||
<Option @click.native="downloadFolder" v-if="isFolder" :title="$t('context_menu.zip_folder')" icon="zip-folder" />
|
||||
<Option @click.native="downloadItem" :title="$t('context_menu.download')" icon="download" />
|
||||
</OptionGroup>
|
||||
|
||||
<!-- Multi options -->
|
||||
@@ -220,9 +216,6 @@ export default {
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
downloadFolder() {
|
||||
this.$store.dispatch('downloadFolder', this.item)
|
||||
},
|
||||
emptyTrash() {
|
||||
this.$store.dispatch('emptyTrash')
|
||||
},
|
||||
@@ -249,8 +242,8 @@ export default {
|
||||
}
|
||||
},
|
||||
downloadItem() {
|
||||
if (this.clipboard.length > 1)
|
||||
this.$store.dispatch('downloadFiles')
|
||||
if (this.clipboard.length > 1 || (this.clipboard.length === 1 && this.clipboard[0].type === 'folder'))
|
||||
this.$store.dispatch('downloadZip')
|
||||
else {
|
||||
this.$downloadFile(this.item.file_url, this.item.name + '.' + this.item.mimetype)
|
||||
}
|
||||
|
||||
@@ -10,8 +10,7 @@
|
||||
</OptionGroup>
|
||||
|
||||
<OptionGroup>
|
||||
<Option v-if="!isFolder" @click.native="downloadItem" :title="$t('context_menu.download')" icon="download" />
|
||||
<Option v-if="isFolder" @click.native="downloadFolder" :title="$t('context_menu.zip_folder')" icon="zip-folder" />
|
||||
<Option @click.native="downloadItem" :title="$t('context_menu.download')" icon="download" />
|
||||
</OptionGroup>
|
||||
</MenuMobileGroup>
|
||||
|
||||
@@ -28,8 +27,7 @@
|
||||
</OptionGroup>
|
||||
|
||||
<OptionGroup>
|
||||
<Option v-if="!isFolder" @click.native="downloadItem" :title="$t('context_menu.download')" icon="download" />
|
||||
<Option v-if="isFolder" @click.native="downloadFolder" :title="$t('context_menu.zip_folder')" icon="zip-folder" />
|
||||
<Option @click.native="downloadItem" :title="$t('context_menu.download')" icon="download" />
|
||||
</OptionGroup>
|
||||
</MenuMobileGroup>
|
||||
|
||||
@@ -47,8 +45,7 @@
|
||||
</OptionGroup>
|
||||
|
||||
<OptionGroup>
|
||||
<Option v-if="!isFolder" @click.native="downloadItem" :title="$t('context_menu.download')" icon="download" />
|
||||
<Option v-if="isFolder" @click.native="downloadFolder" :title="$t('context_menu.zip_folder')" icon="zip-folder" />
|
||||
<Option @click.native="downloadItem" :title="$t('context_menu.download')" icon="download" />
|
||||
</OptionGroup>
|
||||
</MenuMobileGroup>
|
||||
|
||||
@@ -61,16 +58,14 @@
|
||||
</OptionGroup>
|
||||
|
||||
<OptionGroup>
|
||||
<Option v-if="!isFolder" @click.native="downloadItem" :title="$t('context_menu.download')" icon="download" />
|
||||
<Option v-if="isFolder" @click.native="downloadFolder" :title="$t('context_menu.zip_folder')" icon="zip-folder" />
|
||||
<Option @click.native="downloadItem" :title="$t('context_menu.download')" icon="download" />
|
||||
</OptionGroup>
|
||||
</MenuMobileGroup>
|
||||
|
||||
<!--Base location for guest with visit permission-->
|
||||
<MenuMobileGroup v-if="$isThisLocation(['base', 'public']) && $checkPermission('visitor')">
|
||||
<OptionGroup>
|
||||
<Option v-if="!isFolder" @click.native="downloadItem" :title="$t('context_menu.download')" icon="download" />
|
||||
<Option v-if="isFolder" @click.native="downloadFolder" :title="$t('context_menu.zip_folder')" icon="zip-folder" />
|
||||
<Option @click.native="downloadItem" :title="$t('context_menu.download')" icon="download" />
|
||||
</OptionGroup>
|
||||
</MenuMobileGroup>
|
||||
</MenuMobile>
|
||||
@@ -126,9 +121,6 @@ export default {
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
downloadFolder() {
|
||||
this.$store.dispatch('downloadFolder', this.clipboard[0])
|
||||
},
|
||||
addToFavourites() {
|
||||
if (this.favourites && !this.favourites.find(el => el.id === this.clipboard[0].id)) {
|
||||
this.$store.dispatch('addToFavourites', this.clipboard[0])
|
||||
@@ -136,12 +128,13 @@ export default {
|
||||
this.$store.dispatch('removeFromFavourites', this.clipboard[0])
|
||||
}
|
||||
},
|
||||
downloadItem() {
|
||||
this.$downloadFile(
|
||||
this.clipboard[0].file_url,
|
||||
this.clipboard[0].name + '.' + this.clipboard[0].mimetype
|
||||
)
|
||||
}
|
||||
downloadItem() {
|
||||
if (this.clipboard.length > 1 || (this.clipboard.length === 1 && this.clipboard[0].type === 'folder'))
|
||||
this.$store.dispatch('downloadZip')
|
||||
else {
|
||||
this.$downloadFile(this.clipboard[0].file_url, this.clipboard[0].name + '.' + this.clipboard[0].mimetype)
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
|
||||
<ToolbarButton class="action-btn" v-if="!$isThisLocation(['shared']) && $checkPermission('master') || $checkPermission('editor')" source="trash" :class="{'is-inactive' : clipboard.length < 1}" :action="$t('actions.delete')" @click.native="deleteItem" />
|
||||
|
||||
<ToolbarButton class="action-btn" v-if="!$isThisLocation(['shared'])" source="download" :class="{'is-inactive': canDownloadItems}" :action="$t('actions.delete')" @click.native="downloadItem" />
|
||||
<ToolbarButton class="action-btn" v-if="!$isThisLocation(['shared'])" source="download" :action="$t('actions.delete')" @click.native="downloadItem" />
|
||||
|
||||
<ToolbarButton class="action-btn" source="shared-off" @click.native="shareCancel" v-if="$isThisLocation(['shared'])" />
|
||||
|
||||
@@ -16,17 +16,18 @@
|
||||
|
||||
<script>
|
||||
import ToolbarButton from '@/components/FilesView/ToolbarButton'
|
||||
import {events} from '@/bus'
|
||||
import {mapGetters} from 'vuex'
|
||||
import {events} from '@/bus'
|
||||
|
||||
export default {
|
||||
name: 'MultiSelectToolbarMobile',
|
||||
components: {ToolbarButton},
|
||||
components: {
|
||||
ToolbarButton
|
||||
},
|
||||
computed: {
|
||||
...mapGetters(['clipboard']),
|
||||
canDownloadItems() {
|
||||
return this.clipboard.filter(item => item.type === 'folder').length !== 0
|
||||
}
|
||||
...mapGetters([
|
||||
'clipboard'
|
||||
]),
|
||||
},
|
||||
data() {
|
||||
return {
|
||||
@@ -42,8 +43,8 @@ export default {
|
||||
events.$emit('mobileSelecting:stop')
|
||||
},
|
||||
downloadItem() {
|
||||
if (this.clipboard.length > 1)
|
||||
this.$store.dispatch('downloadFiles')
|
||||
if (this.clipboard.length > 1 || (this.clipboard.length === 1 && this.clipboard[0].type === 'folder'))
|
||||
this.$store.dispatch('downloadZip')
|
||||
else {
|
||||
this.$downloadFile(this.clipboard[0].file_url, this.clipboard[0].name + '.' + this.clipboard[0].mimetype)
|
||||
}
|
||||
@@ -54,23 +55,14 @@ export default {
|
||||
events.$emit('popup:open', {name: 'move', item: [this.clipboard[0]]})
|
||||
},
|
||||
deleteItem() {
|
||||
//Delete items
|
||||
this.$store.dispatch('deleteItem')
|
||||
this.closeSelecting()
|
||||
}
|
||||
},
|
||||
created() {
|
||||
events.$on('mobileSelecting:start', () => {
|
||||
this.mobileMultiSelect = true
|
||||
|
||||
})
|
||||
|
||||
events.$on('mobileSelecting:stop', () => {
|
||||
this.mobileMultiSelect = false
|
||||
|
||||
})
|
||||
events.$on('mobileSelecting:start', () => this.mobileMultiSelect = true)
|
||||
events.$on('mobileSelecting:stop', () => this.mobileMultiSelect = false)
|
||||
}
|
||||
|
||||
}
|
||||
</script>
|
||||
|
||||
|
||||
24
resources/js/store/modules/fileFunctions.js
vendored
24
resources/js/store/modules/fileFunctions.js
vendored
@@ -15,26 +15,24 @@ const defaultState = {
|
||||
}
|
||||
|
||||
const actions = {
|
||||
downloadFolder: ({getters}, folder) => {
|
||||
|
||||
let route = getters.sharedDetail
|
||||
? `/api/zip/folder/${folder.id}/${router.currentRoute.params.token}`
|
||||
: `/api/zip/folder/${folder.id}`
|
||||
|
||||
Vue.prototype.$downloadFile(route, 'files.zip')
|
||||
},
|
||||
downloadFiles: ({getters}) => {
|
||||
downloadZip: ({getters}) => {
|
||||
let files = []
|
||||
|
||||
// get ids of selected files
|
||||
getters.clipboard.forEach(file => files.push(file.id))
|
||||
getters.clipboard.forEach(file => {
|
||||
let type = file.type === 'folder'
|
||||
? 'folder'
|
||||
: 'file'
|
||||
|
||||
let ids = files.join(',')
|
||||
files.push(file.id + '|' + type)
|
||||
})
|
||||
|
||||
let items = files.join(',')
|
||||
|
||||
// Get route
|
||||
let route = getters.sharedDetail
|
||||
? `/api/zip/files/${router.currentRoute.params.token}/?ids=${ids}`
|
||||
: `/api/zip/files?ids=${ids}`
|
||||
? `/api/zip/${router.currentRoute.params.token}?items=${items}`
|
||||
: `/api/zip?items=${items}`
|
||||
|
||||
Vue.prototype.$downloadFile(route, 'files.zip')
|
||||
},
|
||||
|
||||
@@ -2,9 +2,8 @@
|
||||
|
||||
use App\Users\Actions\CreateNewUserAction;
|
||||
use Domain\Pages\Controllers\PagesController;
|
||||
use Domain\Zip\Controllers\ZipFilesController;
|
||||
use Domain\Zip\Controllers\ZipController;
|
||||
use Domain\Sharing\Controllers\ShareController;
|
||||
use Domain\Zip\Controllers\ZipFolderController;
|
||||
use Domain\Trash\Controllers\DumpTrashController;
|
||||
use App\Users\Controllers\ResetPasswordController;
|
||||
use Domain\Files\Controllers\UploadFileController;
|
||||
@@ -80,6 +79,5 @@ Route::group(['middleware' => ['auth:sanctum']], function () {
|
||||
Route::post('/remove', DeleteFileOrFolderController::class);
|
||||
Route::post('/move', MoveFileOrFolderController::class);
|
||||
|
||||
Route::get('/zip/folder/{id}', ZipFolderController::class);
|
||||
Route::get('/zip/files', ZipFilesController::class);
|
||||
Route::get('/zip', ZipController::class);
|
||||
});
|
||||
|
||||
86
src/Domain/Zip/Actions/ZipAction.php
Normal file
86
src/Domain/Zip/Actions/ZipAction.php
Normal file
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Domain\Zip\Actions;
|
||||
|
||||
|
||||
use Domain\Folders\Models\Folder;
|
||||
use Domain\Sharing\Models\Share;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use STS\ZipStream\ZipStreamFacade as Zip;
|
||||
use ZipStream\ZipStream;
|
||||
|
||||
class ZipAction
|
||||
{
|
||||
public function __invoke(
|
||||
Collection $folders,
|
||||
Collection $files,
|
||||
?Share $shared = null
|
||||
): ZipStream {
|
||||
|
||||
// Get user id
|
||||
$user_id = Auth::id() ?? $shared->user_id;
|
||||
|
||||
// Create zip
|
||||
$zip = Zip::create('files.zip');
|
||||
|
||||
// Zip Files
|
||||
$files->map(function ($file) use ($zip) {
|
||||
// get file path
|
||||
$filePath = "files/$file->user_id/$file->basename";
|
||||
|
||||
// Add file into zip
|
||||
if (Storage::exists($filePath)) {
|
||||
// local disk
|
||||
if (is_storage_driver('local')) {
|
||||
$zip->add(Storage::path($filePath), $file->name);
|
||||
}
|
||||
|
||||
// s3 client
|
||||
if (is_storage_driver('s3')) {
|
||||
$bucketName = config('filesystems.disks.s3.bucket');
|
||||
|
||||
$zip->add("s3://$bucketName/$filePath", $file->name);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Zip Folders
|
||||
$folders->map(function ($folder) use ($zip, $user_id) {
|
||||
// Get folder
|
||||
$requested_folder = Folder::with(['folders.files', 'files'])
|
||||
->where('id', $folder->id)
|
||||
->where('user_id', $user_id)
|
||||
->with('folders')
|
||||
->first();
|
||||
|
||||
$folderFiles = get_files_for_zip($requested_folder, collect([]));
|
||||
|
||||
foreach ($folderFiles as $file) {
|
||||
// get file path
|
||||
$filePath = "files/$user_id/{$file['basename']}";
|
||||
|
||||
// Add file into zip
|
||||
if (Storage::exists($filePath)) {
|
||||
$zipDestination = "{$file['folder_path']}/{$file['name']}";
|
||||
|
||||
// local disk
|
||||
if (is_storage_driver('local')) {
|
||||
$zip->add(Storage::path($filePath), $zipDestination);
|
||||
}
|
||||
|
||||
// s3 client
|
||||
if (is_storage_driver('s3')) {
|
||||
$bucketName = config('filesystems.disks.s3.bucket');
|
||||
|
||||
$zip->add("s3://$bucketName/$filePath", $zipDestination);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return $zip;
|
||||
}
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
<?php
|
||||
namespace Domain\Zip\Actions;
|
||||
|
||||
use STS\ZipStream\ZipStream;
|
||||
use Domain\Sharing\Models\Share;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use STS\ZipStream\ZipStreamFacade as Zip;
|
||||
|
||||
class ZipFilesAction
|
||||
{
|
||||
/**
|
||||
* Zip selected files, and download it as response stream
|
||||
*/
|
||||
public function __invoke(
|
||||
File | Collection $files,
|
||||
?Share $shared = null,
|
||||
): ZipStream {
|
||||
// Create zip
|
||||
$zip = Zip::create('files.zip');
|
||||
|
||||
$files->map(function ($file) use ($zip) {
|
||||
// get file path
|
||||
$filePath = "files/$file->user_id/$file->basename";
|
||||
|
||||
// Add file into zip
|
||||
if (Storage::exists($filePath)) {
|
||||
// local disk
|
||||
if (is_storage_driver('local')) {
|
||||
$zip->add(Storage::path($filePath), $file->name);
|
||||
}
|
||||
|
||||
// s3 client
|
||||
if (is_storage_driver('s3')) {
|
||||
$bucketName = config('filesystems.disks.s3.bucket');
|
||||
|
||||
$zip->add("s3://$bucketName/$filePath", $file->name);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return $zip;
|
||||
}
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
<?php
|
||||
namespace Domain\Zip\Actions;
|
||||
|
||||
use STS\ZipStream\ZipStream;
|
||||
use Domain\Sharing\Models\Share;
|
||||
use Domain\Folders\Models\Folder;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use STS\ZipStream\ZipStreamFacade as Zip;
|
||||
|
||||
class ZipFolderAction
|
||||
{
|
||||
/**
|
||||
* Zip requested folder
|
||||
*/
|
||||
public function __invoke(
|
||||
$id,
|
||||
?Share $shared = null
|
||||
): ZipStream {
|
||||
$user_id = Auth::id() ?? $shared->user_id;
|
||||
|
||||
// Get folder
|
||||
$requested_folder = Folder::with(['folders.files', 'files'])
|
||||
->where('id', $id)
|
||||
->where('user_id', $user_id)
|
||||
->with('folders')
|
||||
->first();
|
||||
|
||||
$files = get_files_for_zip($requested_folder, collect([]));
|
||||
|
||||
// Create zip
|
||||
$zip = Zip::create('files.zip');
|
||||
|
||||
foreach ($files as $file) {
|
||||
// get file path
|
||||
$filePath = "files/$user_id/{$file['basename']}";
|
||||
|
||||
// Add file into zip
|
||||
if (Storage::exists($filePath)) {
|
||||
$zipDestination = "{$file['folder_path']}/{$file['name']}";
|
||||
|
||||
// local disk
|
||||
if (is_storage_driver('local')) {
|
||||
$zip->add(Storage::path($filePath), $zipDestination);
|
||||
}
|
||||
|
||||
// s3 client
|
||||
if (is_storage_driver('s3')) {
|
||||
$bucketName = config('filesystems.disks.s3.bucket');
|
||||
|
||||
$zip->add("s3://$bucketName/$filePath", $zipDestination);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $zip;
|
||||
}
|
||||
}
|
||||
65
src/Domain/Zip/Controllers/ZipController.php
Normal file
65
src/Domain/Zip/Controllers/ZipController.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Domain\Zip\Controllers;
|
||||
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Domain\Files\Models\File;
|
||||
use Domain\Folders\Models\Folder;
|
||||
use Domain\Traffic\Actions\RecordDownloadAction;
|
||||
use Domain\Zip\Actions\ZipAction;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use STS\ZipStream\ZipStream;
|
||||
|
||||
class ZipController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
public ZipAction $zip,
|
||||
public RecordDownloadAction $recordDownload,
|
||||
) {}
|
||||
|
||||
public function __invoke(
|
||||
Request $request,
|
||||
): ZipStream {
|
||||
|
||||
$user_id = Auth::id();
|
||||
$items = explode(',', $request->get('items'));
|
||||
|
||||
$itemList = collect($items)
|
||||
->map(function ($chunk) {
|
||||
$items = explode('|', $chunk);
|
||||
|
||||
return [
|
||||
'id' => $items[0],
|
||||
'type' => $items[1],
|
||||
];
|
||||
});
|
||||
|
||||
$folderIds = $itemList
|
||||
->where('type', 'folder')
|
||||
->pluck('id');
|
||||
|
||||
$fileIds = $itemList
|
||||
->where('type', 'file')
|
||||
->pluck('id');
|
||||
|
||||
$folders = Folder::whereUserId($user_id)
|
||||
->whereIn('id', $folderIds)
|
||||
->get();
|
||||
|
||||
$files = File::whereUserId($user_id)
|
||||
->whereIn('id', $fileIds)
|
||||
->get();
|
||||
|
||||
$zip = ($this->zip)($folders, $files);
|
||||
|
||||
($this->recordDownload)(
|
||||
file_size: $zip->predictZipSize(),
|
||||
user_id: $user_id,
|
||||
);
|
||||
|
||||
return $zip;
|
||||
}
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
<?php
|
||||
namespace Domain\Zip\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use STS\ZipStream\ZipStream;
|
||||
use Domain\Files\Models\File;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Domain\Zip\Actions\ZipFilesAction;
|
||||
use Domain\Traffic\Actions\RecordDownloadAction;
|
||||
|
||||
class ZipFilesController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private ZipFilesAction $zipFiles,
|
||||
private RecordDownloadAction $recordDownload,
|
||||
) {
|
||||
}
|
||||
|
||||
public function __invoke(
|
||||
Request $request,
|
||||
): ZipStream {
|
||||
$files = File::whereUserId(Auth::id())
|
||||
->whereIn('id', explode(',', $request->get('ids')))
|
||||
->get();
|
||||
|
||||
$zip = ($this->zipFiles)($files);
|
||||
|
||||
($this->recordDownload)(
|
||||
file_size: $zip->predictZipSize(),
|
||||
user_id: Auth::id(),
|
||||
);
|
||||
|
||||
return $zip;
|
||||
}
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
<?php
|
||||
namespace Domain\Zip\Controllers;
|
||||
|
||||
use STS\ZipStream\ZipStream;
|
||||
use Domain\Folders\Models\Folder;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Domain\Zip\Actions\ZipFolderAction;
|
||||
use Domain\Traffic\Actions\RecordDownloadAction;
|
||||
|
||||
class ZipFolderController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private ZipFolderAction $zipFolder,
|
||||
private RecordDownloadAction $recordDownload,
|
||||
) {
|
||||
}
|
||||
|
||||
public function __invoke(
|
||||
string $id,
|
||||
): ZipStream {
|
||||
$folder = Folder::whereUserId(Auth::id())
|
||||
->where('id', $id);
|
||||
|
||||
if (! $folder->exists()) {
|
||||
response("Requested folder doesn't exists.", 404);
|
||||
}
|
||||
|
||||
$zip = ($this->zipFolder)($id);
|
||||
|
||||
($this->recordDownload)(
|
||||
file_size: $zip->predictZipSize(),
|
||||
user_id: Auth::id(),
|
||||
);
|
||||
|
||||
return $zip;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Domain\Zip;
|
||||
|
||||
use Storage;
|
||||
@@ -21,6 +22,24 @@ class UserZippingTest extends TestCase
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$folder = Folder::factory(Folder::class)
|
||||
->create([
|
||||
'user_id' => $user->id,
|
||||
]);
|
||||
|
||||
collect([0, 1])
|
||||
->each(function ($index) use ($folder) {
|
||||
$file = UploadedFile::fake()
|
||||
->create("fake-inner-file-$index.pdf", 1200, 'application/pdf');
|
||||
|
||||
$this->postJson('/api/upload', [
|
||||
'filename' => $file->name,
|
||||
'file' => $file,
|
||||
'folder_id' => $folder->id,
|
||||
'is_last' => 'true',
|
||||
])->assertStatus(201);
|
||||
});
|
||||
|
||||
collect([0, 1])
|
||||
->each(function ($index) {
|
||||
$file = UploadedFile::fake()
|
||||
@@ -34,12 +53,13 @@ class UserZippingTest extends TestCase
|
||||
])->assertStatus(201);
|
||||
});
|
||||
|
||||
$file_ids = File::all()->pluck('id')->toArray();
|
||||
|
||||
$ids = implode(',', $file_ids);
|
||||
$files = File::all()
|
||||
->where('folder_id', null)
|
||||
->pluck('id')
|
||||
->toArray();
|
||||
|
||||
$this
|
||||
->getJson("/api/zip/files?ids=$ids")
|
||||
->getJson("/api/zip?items=$files[0]|file,$files[1]|file,$folder->id|folder")
|
||||
->assertStatus(200)
|
||||
->assertHeader('content-type', 'application/x-zip');
|
||||
}
|
||||
@@ -72,7 +92,7 @@ class UserZippingTest extends TestCase
|
||||
])->assertStatus(201);
|
||||
});
|
||||
|
||||
$this->getJson("/api/zip/folder/$folder->id")
|
||||
$this->getJson("/api/zip?items=$folder->id|folder")
|
||||
->assertStatus(200)
|
||||
->assertHeader('content-type', 'application/x-zip');
|
||||
}
|
||||
|
||||
@@ -25,6 +25,6 @@ abstract class TestCase extends BaseTestCase
|
||||
|
||||
resolve(CreateDiskDirectoriesAction::class)();
|
||||
|
||||
$this->withoutExceptionHandling();
|
||||
//$this->withoutExceptionHandling();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user