|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2020 Google LLC. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +// [START functions_http_form_data] |
| 19 | + |
| 20 | +use Psr\Http\Message\ServerRequestInterface; |
| 21 | +use Psr\Http\Message\ResponseInterface; |
| 22 | +use GuzzleHttp\Psr7\Response; |
| 23 | + |
| 24 | +function uploadFile(ServerRequestInterface $request): ResponseInterface |
| 25 | +{ |
| 26 | + if ($request->getMethod() != 'POST') { |
| 27 | + return new Response(405, [], 'Method Not Allowed: expected POST, found ' . $request->getMethod()); |
| 28 | + } |
| 29 | + |
| 30 | + $contentType = $request->getHeader('Content-Type')[0]; |
| 31 | + if (strpos($contentType, 'multipart/form-data') !== 0) { |
| 32 | + return new Response(400, [], 'Bad Request: content of type "multipart/form-data" not provided, found ' . $contentType); |
| 33 | + } |
| 34 | + |
| 35 | + $fileList = []; |
| 36 | + /** @var $file Psr\Http\Message\UploadedFileInterface */ |
| 37 | + foreach ($request->getUploadedFiles() as $name => $file) { |
| 38 | + // Use caution when trusting the client-provided filename: |
| 39 | + // https://owasp.org/www-community/vulnerabilities/Unrestricted_File_Upload |
| 40 | + $fileList[] = $file->getClientFilename(); |
| 41 | + |
| 42 | + infoLog('Processing ' . $file->getClientFilename()); |
| 43 | + $filename = tempnam(sys_get_temp_dir(), $name . '.') . '-' . $file->getClientFilename(); |
| 44 | + |
| 45 | + // Use $file->getStream() to process the file contents in ways other than a direct "file save". |
| 46 | + infoLog('Saving to ' . $filename); |
| 47 | + $file->moveTo($filename); |
| 48 | + } |
| 49 | + |
| 50 | + if (empty($fileList)) { |
| 51 | + $msg = 'Bad Request: no files sent for upload'; |
| 52 | + errorLog($msg); |
| 53 | + return new Response(400, [], $msg); |
| 54 | + } |
| 55 | + |
| 56 | + return new Response(201, [], 'Saved ' . join(', ', $fileList)); |
| 57 | +} |
| 58 | + |
| 59 | +function errorLog($msg): void |
| 60 | +{ |
| 61 | + $stream = fopen('php://stderr', 'wb'); |
| 62 | + $entry = json_encode(['msg' => $msg, 'severity' => 'error'], JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES); |
| 63 | + fwrite($stream, $entry . PHP_EOL); |
| 64 | +} |
| 65 | + |
| 66 | +function infoLog($msg): void |
| 67 | +{ |
| 68 | + $stream = fopen('php://stderr', 'wb'); |
| 69 | + $entry = json_encode(['message' => $msg, 'severity' => 'info'], JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES); |
| 70 | + fwrite($stream, $entry . PHP_EOL); |
| 71 | +} |
| 72 | + |
| 73 | +// [END functions_http_form_data] |
0 commit comments