Skip to content

Commit 561d7fe

Browse files
committed
Improve zero configuration usage with session detection
Now if the session can be used, it will fallback to browser instead to prevent incorrect usage.
1 parent e5cbd6b commit 561d7fe

File tree

2 files changed

+41
-50
lines changed

2 files changed

+41
-50
lines changed

readme.md

Lines changed: 15 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Easy to use service for chunked upload with several js providers on top of Larav
1717
* [Laravel controller](#laravel-controller)
1818
* [Route](#route)
1919
* [Providers/Handlers](#providers-handlers)
20-
* [Changelog](#changelog)
20+
* [Changelog](https://github.com/pionl/laravel-chunk-upload/releases)
2121
* [Contribution or overriding](#contribution-or-overriding)
2222
* [Suggested frontend libs](#suggested-frontend-libs)
2323

@@ -58,7 +58,7 @@ php artisan vendor:publish --provider="Pion\Laravel\ChunkUpload\Providers\ChunkU
5858
* **Chunked uploads**
5959
uses **chunked writing** aswell to minimize the memory footprint
6060
* **Storing per Laravel Session to prevent overwrite**
61-
all TMP files are stored with session token
61+
all TMP files are stored with session token. The JS library must send the **cookies** to successfully work (or you can use browser - with fallback support).
6262
* [**Clear command and schedule**](#uploads:clear)
6363
the package registers the shedule command (uploads:clear) that will clear all unfinished chunk uploads
6464
* **Automatic handler selection** since `v0.2.4` you can use automatic detection selection the handler
@@ -71,7 +71,7 @@ to use from the current supported providers. You can also register your own hand
7171
1. Create a Upload controller. If using Laravel 5.4 and above, add your upload controller into `web` route. If
7272
necessary, add to `api` routes and change the config to use IP for chunk name.
7373
2. Implement your Javascript code (you can use the same code as below or in example repository)
74-
3. __Check if your library is sending `cookie`, the chunk naming uses session (you can [change it](#unique-naming) - will use only IP address)__
74+
3. __Check if your library is sending `cookie` in post, the chunk naming uses session (you can [change it](#unique-naming) - will use only IP address)__
7575
4. Implement the FileReceiver (example below).
7676
**Chunk upload works only withing local storage.** If you need to upload the file to the cloud you can do it only after the chunks are merged `$receiver->isFinished() === true`. Instead of using
7777
`move` function get the contents of your file and upload it to cloud. More can be found (here)[https://github.com/pionl/laravel-chunk-upload-example/issues/5#issuecomment-359793775]
@@ -96,17 +96,25 @@ which does nothing at this moment.
9696

9797
The full example (Laravel 5.4 - works same on previous versions) can be found in separate repository [laravel-chunk-upload-example](https://github.com/pionl/laravel-chunk-upload-example)
9898
with DropZone and jQuery-File-Upload implementation.
99+
100+
### JS Libraries
101+
102+
- Dropzone [Code](https://github.com/pionl/laravel-chunk-upload-example/blob/master/resources/assets/js/dropzone.js) [View](https://github.com/pionl/laravel-chunk-upload-example/blob/master/resources/views/example/dropzone.blade.php)
103+
- Resumable [Code](https://github.com/pionl/laravel-chunk-upload-example/blob/master/resources/assets/js/resumable.js) [View](https://github.com/pionl/laravel-chunk-upload-example/blob/master/resources/views/example/resumable.blade.php)
104+
- jQuery-File-Upload [Code](https://github.com/pionl/laravel-chunk-upload-example/blob/master/resources/assets/js/jquery-file-upload.js) [View](https://github.com/pionl/laravel-chunk-upload-example/blob/master/resources/views/example/jquery-file-upload.blade.php)
99105

106+
You must send `_token` if you are using web route. For `api` session is not used (must be inited by middleware).
107+
100108
### Laravel controller
101109
* Create laravel controller `UploadController` and create the file receiver with the desired handler.
102110
* You must import the full namespace in your controller (`use`).
103111
* When upload is finished, don't forget to **move the file to desired folder (as standard UploadFile implementation)**.
104-
You can check the example project.
112+
You can check the example project. If you are uploading the contents of the file to cloud, don`t forget to delete it.
105113
* An example of save function below the handler usage
106114

107115
#### Dynamic handler usage
108116

109-
The correct handler for your JS provider will be selected automatically based on the sent request. This is the easies init.
117+
The correct handler for your JS provider will be selected automatically based on the sent request. This is the easies usage.
110118

111119
##### With dependency injection
112120

@@ -323,7 +331,8 @@ The logic supports also using the `Session::getId()`, but you need to force your
323331
You can update the `chunk.name.use` settings for custom usage.
324332

325333
#### Cross domain request
326-
When using uploader for the cross domain request you must setup the `chunk.name.use` to browser logic instead of session.
334+
When using uploader for the cross domain request you must setup the `chunk.name.use` to browser logic instead of session. From version `1.1.4` the session
335+
is uses only if initialized by the laravel (if using api endpoint it will not be initialized and browser data will be used as fallback).
327336

328337
"use" => [
329338
"session" => false, // should the chunk name use the session id? The uploader muset send cookie!,
@@ -384,48 +393,6 @@ or pass as second parameter when using
384393
HandlerFactory::classFromRequest($request, CustomHandler::class)
385394
```
386395

387-
## Changelog
388-
389-
### Since 1.1.3
390-
* Added DropZone support (#22)
391-
* Removed Laravel dependency in favor of Illuminate packages (#21)
392-
393-
### Since 1.1.2
394-
* Added support for Auto-Discovery (thanks to @laravelish - [#20](https://github.com/pionl/laravel-chunk-upload/pull/20))
395-
396-
### Since 1.1.1
397-
* Added support for Laravel 5.5 (thanks to @Colbydude - [#18](https://github.com/pionl/laravel-chunk-upload/pull/18))
398-
399-
### Since 1.1.0
400-
* If there is an error while upload, exception will be thrown on init.
401-
402-
### Since 1.0.3
403-
* Enabled to construct the `FileReceiver` with dependency injection - the fasted way.
404-
* Removed the `getChunkFile` and added `getUploadedFile` for all Save classes. Returns always the uploaded file (the uploaded chunk).
405-
406-
### Since 1.0.2
407-
* Added resumable.js
408-
* Added `getChunkFile` method in `ChunkSave` for returning only the chunk file
409-
410-
### Since 1.0.1
411-
* Added support for passing file object instead of fileIndex (example: multiple files in a request). Change discussion in #7 (@RAZORzdenko), merged in #8
412-
413-
### Since 1.0.0
414-
415-
* Updated composer to support Laravel 5.4
416-
417-
### Since v0.3
418-
419-
* Support for cross domain requests (only chunk naming)
420-
* Added support for [plupload package](https://github.com/moxiecode/plupload)
421-
* Added automatic handler selection based on the request
422-
423-
### Since v0.2.0
424-
425-
The package supports the Laravel Filesystem. Because of this, the storage must be withing the app folder `storage/app/` or custom drive (only local) - can be set in the config `storage.disk`.
426-
427-
The cloud drive is not supported because of the chunked write (probably could be changed to use a stream) and the resulting object - `UploadedFile` that supports only full path.
428-
429396
## Todo
430397

431398
- [ ] add more providers

src/Handler/AbstractHandler.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,24 @@ public static function canBeUsedForRequest(Request $request)
5555
return false;
5656
}
5757

58+
/**
59+
* Checks the current setup if session driver was booted - if not, it will generate random hash
60+
* @return bool
61+
*/
62+
static public function canUseSession()
63+
{
64+
// Get the session driver and check if it was started - fully inited by laravel
65+
$session = session();
66+
$driver = $session->getDefaultDriver();
67+
$drivers = $session->getDrivers();
68+
69+
// Check if the driver is valid and started - allow using session
70+
if (isset($drivers[$driver]) && $drivers[$driver]->isStarted() === true) {
71+
return true;
72+
}
73+
return false;
74+
}
75+
5876
/**
5977
* Builds the chunk file name per session and the original name. You can
6078
* provide custom additional name at the end of the generated file name. All chunk
@@ -75,14 +93,20 @@ protected function createChunkFileName($additionalName = null)
7593
];
7694

7795
// ensure that the chunk name is for unique for the client session
96+
$useSession = $this->config->chunkUseSessionForName();
97+
$useBrowser = $this->config->chunkUseBrowserInfoForName();
98+
if ($useSession && $this->canUseSession() === false) {
99+
$useBrowser = true;
100+
$useSession = false;
101+
}
78102

79103
// the session needs more config on the provider
80-
if ($this->config->chunkUseSessionForName()) {
104+
if ($useSession) {
81105
$array[] = Session::getId();
82106
}
83107

84108
// can work without any additional setup
85-
if ($this->config->chunkUseBrowserInfoForName()) {
109+
if ($useBrowser) {
86110
$array[] = md5($this->request->ip().$this->request->header("User-Agent", "no-browser"));
87111
}
88112

0 commit comments

Comments
 (0)