Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ファイル管理で複数ファイルのアップロードを実装 #4235

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 24 additions & 9 deletions src/Eccube/Controller/Admin/Content/FileController.php
Expand Up @@ -55,7 +55,12 @@ public function __construct()
public function index(Request $request)
{
$form = $this->formFactory->createBuilder(FormType::class)
->add('file', FileType::class)
->add('file', FileType::class, [
'multiple' => true,
'attr' => [
'multiple' => 'multiple'
],
])
->add('create_file', TextType::class)
->getForm();

Expand Down Expand Up @@ -132,7 +137,12 @@ public function view(Request $request)
public function create(Request $request)
{
$form = $this->formFactory->createBuilder(FormType::class)
->add('file', FileType::class)
->add('file', FileType::class, [
'multiple' => true,
'attr' => [
'multiple' => 'multiple'
],
])
->add('create_file', TextType::class, [
'constraints' => [
new Assert\NotBlank(),
Expand Down Expand Up @@ -238,6 +248,7 @@ public function upload(Request $request)
{
$form = $this->formFactory->createBuilder(FormType::class)
->add('file', FileType::class, [
'multiple' => true,
'constraints' => [
new Assert\NotBlank([
'message' => 'admin.common.file_select_empty',
Expand All @@ -263,16 +274,20 @@ public function upload(Request $request)

if (!$this->checkDir($nowDir, $topDir)) {
$this->errors[] = ['message' => 'file.text.error.invalid_upload_folder'];

return;
}

$filename = $this->convertStrToServer($data['file']->getClientOriginalName());
try {
$data['file']->move($nowDir, $filename);
$isUploaded = false;
foreach ($data['file'] as $file) {
$filename = $this->convertStrToServer($file->getClientOriginalName());
try {
$file->move($nowDir, $filename);
$isUploaded = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kazumiiiiiiiiiii
成功/失敗のメッセージが混在することになるので、表示のしかたをご検討いただけますか。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

成功した場合のメッセージに、成功したファイルアップロード数を追加しました。
スクリーンショット 2019-08-02 18 37 37

失敗した際のエラーメッセージには、失敗したファイル名を表示するように修正を行いました。

} catch (FileException $e) {
$this->errors[] = ['message' => $e->getMessage()];
}
}
if ($isUploaded) {
$this->addSuccess('admin.common.upload_complete', 'admin');
} catch (FileException $e) {
$this->errors[] = ['message' => $e->getMessage()];
}
}

Expand Down
27 changes: 20 additions & 7 deletions tests/Eccube/Tests/Web/Admin/Content/FileControllerTest.php
Expand Up @@ -94,35 +94,48 @@ public function testIndexWithCreate()

public function testIndexWithUpload()
{
$filepath = $this->getUserDataDir().'/../aaa.html';
$contents = '<html><body><h1>test</h1></body></html>';
file_put_contents($filepath, $contents);
$filepath1 = $this->getUserDataDir().'/../aaa.html';
$contents1 = '<html><body><h1>test1</h1></body></html>';
file_put_contents($filepath1, $contents1);

$file = new UploadedFile(
realpath($filepath), // file path
$filepath2 = $this->getUserDataDir().'/../bbb.html';
$contents2 = '<html><body><h1>test2</h1></body></html>';
file_put_contents($filepath2, $contents2);

$file1 = new UploadedFile(
realpath($filepath1), // file path
'aaa.html', // original name
'text/html', // mimeType
null, // file size
null, // error
true // test mode
);
$file2 = new UploadedFile(
realpath($filepath2), // file path
'bbb.html', // original name
'text/html', // mimeType
null, // file size
null, // error
true // test mode
);
$crawler = $this->client->request(
'POST',
$this->generateUrl('admin_content_file'),
[
'form' => [
'_token' => 'dummy',
'create_file' => '',
'file' => $file,
'file' => [$file1, $file2],
],
'mode' => 'upload',
'now_dir' => '/',
],
['file' => $file]
['file' => [$file1, $file2]]
);

$this->assertTrue($this->client->getResponse()->isSuccessful());
$this->assertTrue(file_exists($this->getUserDataDir().'/aaa.html'));
$this->assertTrue(file_exists($this->getUserDataDir().'/bbb.html'));
}

protected function getUserDataDir()
Expand Down