Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[FrameworkBundle] Add file helper to Controller#18502

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed
dfridrich wants to merge24 commits intosymfony:masterfromdfridrich:file_helper

Conversation

@dfridrich
Copy link
Contributor

@dfridrichdfridrich commentedApr 10, 2016
edited
Loading

QA
Branch?master
Bug fix?no
New feature?yes
BC breaks?no
Deprecations?no
Tests pass?yes
Fixed tickets
LicenseMIT
Doc PRsymfony/symfony-docs#6454

I think it would be more "sexy" to serve files from controller easier (likejson() helper does).

This Controller helper allows user to serve files to Response in these ways:

  • passSymfony\Component\HttpFoundation\File (orSymfony\Component\HttpFoundation\UploadedFile) instance
  • [REMOVED] provide content asstring and specify file name (mime type will be auto recognized)
  • provide path to file (you are still able to specify other than original file name)

Examples

return $this->file($uploadedFile);// ...or...return $this->file('/path/to/my/picture.jpg');

Zemistr, MortalFlesh, HeahDude, and TomasVotruba reacted with thumbs up emoji
@javiereguiluz
Copy link
Member

@dfridrich thanks for sending this contribution to improve Symfony (and its associated docs too).

Just a comment for your next contributions: it's always better to open an issue to propose new features without sending the PR to implement them. That allows the community to review your idea and propose changes to it. Besides, if the idea is ultimately rejected, creating a PR will make you waste your time whereas creating an issue is quick and easy. Thanks!

@dfridrich
Copy link
ContributorAuthor

@javiereguiluz thanks for information, I'll remember next time.

$response =newBinaryFileResponse($file);
$mimeType =$file->getMimeType();
}elseif (is_string($file)) {
if ($mimeType ===null) {
Copy link
Contributor

Choose a reason for hiding this comment

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

you could use the MimeTypeGuesser here

dfridrich and Zemistr reacted with thumbs up emoji
Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

@Nicofuma thx, MimeTypeGuesser is now implemented (using BinaryFileResponse class)

@dfridrich
Copy link
ContributorAuthor

dfridrich commentedMay 2, 2016
edited
Loading

I think this my PR is really very useful, what do you think @symfony/deciders?

/**
* Returns a BinaryFileResponse object with original or customized file name and disposition header.
*
* @param File|string $file File object, path to file or string with content to be sent as response
Copy link
Contributor

Choose a reason for hiding this comment

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

Should be@param string|null

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

@HeahDude you are right, thx

@HeahDude
Copy link
Contributor

Besides my minor comments, I think this is a nice addition. Thanks :)

@dfridrich
Copy link
ContributorAuthor

@HeahDude Thanks for your feedback, I really appreciate it! I just commited fixed version of this great feature 👻

HeahDude reacted with laugh emoji

}else {
thrownew \InvalidArgumentException(
sprintf(
'"%s" can\'t be passed as parameter to "%s" method of "%s" class.',
Copy link
Contributor

Choose a reason for hiding this comment

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

I've no strong opinion about this either, it just seems to me (IIUC) that at this point$file as string has been transformed to aFile object, so it means if you got something else and you cannot be sure it's a scalar.
One other way would be to throw an\InvalidArgumentException when notstring orFile at the very beginning of the method.

@jvasseur
Copy link
Contributor

I don't think allowing to pass a string containing the file contents is a good idea. If you already have the content of the response, why storing it in a temporary file to then read back from it ?

Plus this means passing a string as a first argument can mean two totally different things, which means if for example you make a typo in the name of the file you want to send you will get this name as a file instead of an error.

@xabbuh
Copy link
Member

I agree with@jvasseur. We had something similar in the YAML parser in the past which was not a good idea (we cleared things in 3.0). We should simply not support passing file contents here.

$mimeType =$file->getMimeType();

$response->headers->set('Content-Type',$mimeType);
$disposition =$response->headers->makeDisposition($disposition,$fileName ===null ?$file->getFileName() :$fileName);
Copy link
Member

Choose a reason for hiding this comment

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

It would be better to use thesetContentDisposition() ofBinaryFileResponse:

$response->setContentDisposition($disposition,$fileName ===null ?$file->getFileName() :$fileName);

if (!$fileinstanceof File) {
$file =newFile($file);
}

Copy link
Member

Choose a reason for hiding this comment

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

As I commented after your changes, the content-type logic is already done in BinaryFileResponse, so this can be removed.

}

$response =newBinaryFileResponse($file,200, ['Content-type' =>$file->getMimeType()]);
$response->setContentDisposition($disposition,$fileName ===null ?$file->getFileName() :$fileName);
Copy link
Member

Choose a reason for hiding this comment

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

This line should be removed.

@fabpot
Copy link
Member

The method should read as follows:

protectedfunctionfile($file,$fileName =null,$disposition = ResponseHeaderBag::DISPOSITION_ATTACHMENT)    {$response =newBinaryFileResponse($file);$response->setContentDisposition($disposition,null ===$fileName ?$file->getFileName() :$fileName);return$response;    }

@jvasseur
Copy link
Contributor

@fabpot your version doesn't work if$file is a string and$fileName is null.

Maybe something like this:

protectedfunctionfile($file,$fileName ='',$disposition = ResponseHeaderBag::DISPOSITION_ATTACHMENT)    {$response =newBinaryFileResponse($file);$response->setContentDisposition($disposition,$fileName);return$response;    }

$this->assertSame('text/x-php',$response->headers->get('content-type'));
}
$this->assertContains(ResponseHeaderBag::DISPOSITION_ATTACHMENT,$response->headers->get('content-disposition'));
$this->assertContains(pathinfo(__FILE__,PATHINFO_BASENAME),$response->headers->get('content-disposition'));
Copy link
Member

Choose a reason for hiding this comment

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

The functionbasename is simpler thanpathinfofor such thing.

dfridrich reacted with thumbs up emoji
@fabpot
Copy link
Member

Thank you@dfridrich.


/**
* Returns a BinaryFileResponse object with original or customized file name and disposition header.
*
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this be\SplFileInfo|string to be equal toBinaryFileResponse?

Copy link
Member

Choose a reason for hiding this comment

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

good catch, see#19115

apfelbox reacted with thumbs up emoji
fabpot added a commit that referenced this pull requestJun 20, 2016
This PR was merged into the 3.2-dev branch.Discussion----------[FrameworkBundle] fix argument type docblock| Q             | A| ------------- | ---| Branch?       | master| Bug fix?      | no| New feature?  | no| BC breaks?    | no| Deprecations? | no| Tests pass?   | yes| Fixed tickets |#18502 (comment)| License       | MIT| Doc PR        |Commits-------c4e440c [FrameworkBundle] fix argument type docblock
* @param string $disposition Disposition of response ("attachment" is default, other type is "inline")
*
* @return BinaryFileResponse
*/
Copy link
Contributor

Choose a reason for hiding this comment

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

doesnt this new method manifest as a BC break when the subclassing controller already contains afile method?

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

11 participants

@dfridrich@javiereguiluz@HeahDude@jvasseur@xabbuh@GromNaN@fabpot@staabm@Nicofuma@apfelbox@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp