【发布时间】:2020-07-01 08:14:05
【问题描述】:
我有一个用于上传视频的 API,这会给出 JSON 响应
success": {
"uid": "****************",
"original_name": "**********.mp4",
"duration": "****"
}
我想要另一个 API 在收到此响应后更新视频的其他数据。我不知道如何进行此操作。
function upload(Request $request)
{
$videofile = $request->file('video');
$validator = Validator::make($request->all(), [
'video' => ['required', 'file', 'mimes:video/mp4,video/mpeg,video/x-matroska,mp4,mov,ogg,video/3gpp,video/3gpp2,video/x-flv,application/x-mpegURL,video/MP2T,video/quicktime,video/x-msvideo,video/x-ms-wmv,application/octet-stream,video/x-ms-asf,image/x-tga'],
]);
if ($validator->fails()) {
return Response::json(array('error' > true, 'msg' => 'Please choose the video to upload'));
} else {
if ($videofile) {
$filename = md5_file($videofile->getRealPath());
$extension = $request->file('video')->getClientOriginalExtension();
$videoId = $filename . time();
$uniqueFilename = $videoId . '.' . $extension;
$path = public_path() . '/videos/';
if ($videofile->move($path, $uniqueFilename)) {
// get video duration
$getID3 = new \getID3;
$videoToUpload = $request->file('video');
$videoPath = public_path() . '/videos/' . $uniqueFilename;
$file = $getID3->analyze($videoPath);
$playtime_seconds = $file['playtime_seconds'];
$videoDuration = gmdate("H:i:s", $playtime_seconds);
$myVideoHour = gmdate('H', strtotime($videoDuration));
if ($myVideoHour == 00) {
$myVideoDuration = gmdate('i:s', strtotime($videoDuration));
} else {
$myVideoDuration = $videoDuration;
}
$output = array(
'uid' => $videoId,
'original_name' => $uniqueFilename,
'duration' => $videoDuration
);
return response()->json(['success' => $output], $this->successStatus);
}
}
}
}
如何在另一个函数中传递这个输出来更新视频的其他数据。
我的另一个功能是---
function uploadData(Output $output)
{
if ($output) {
$validator = Validator::make($request->all(), [
'title' => ['required', 'string', 'max:255'],
'keywords' => ['required', 'string', 'max:255'],
'channel_id' => ['required', 'not_in:0'],
'description' => ['max:2000'],
'visibility' => ['required', 'in:public,unlisted,private'],
'thumbnail' => ['image', 'mimes:jpeg,png,jpg,gif', 'max:6144', 'nullable'],
'uid' => ['max:2000'],
'original_name' => ['max:2000'],
'duration' => ['max:2000'],
]);
if ($validator->fails()) {
return response()->json(['error' => $validator->errors()], 401);
}
$video = new Video;
$user = Auth::user();
// print_r($user);
// exit;
$user_id = $user->id;
// $user_id = auth()->user()->id;
$Channels = Channel::Where('channels.user_id', $user_id)->get();
$video->user_id = $user_id;
$video->uid = $videoId;
$video->title = $request->title;
$video->original_name = $uniqueFilename;
$video->keywords = $request->keywords;
$video->thumbnail_url = $request->thumbnail_url;
$video->description = $request->description;
$video->visibility = $request->visibility;
$video->category_id = $request->category_id;
$video->channel_id = $request->channel_id;
$video->duration = $videoDuration;
$video->allow_votes = $request->has('allow_votes');
$video->allow_comments = $request->has('allow_comments');
if (!empty($request->get('playlistid'))) {
$video->playlist_id = $request->get('playlistid');
} elseif (!empty($request->playlist_id)) {
$video->playlist_id = $request->playlist_id;
}
$filename = '';
$site_url = url('/');
if ($request->hasFile('thumbnail')) {
$image = $request->file('thumbnail');
$filename = $video->id . '_' . time() . '_' . uniqid(rand()) . '.' . $image->getClientOriginalExtension();
$img = Image::make($image->getRealPath())->resize(720, 404);
//$img->save($filename);
$path = 'images/thumbnails/';
$img->save("$path$filename");
$url = $site_url . '/' . $path;
$thumbnails = $url . $filename;
$video->thumbnail_url = $thumbnails;
}
$videoUrl = url('/videos/' . $request->uid);
$video->save();
return response()->json([
"message" => "video record created"
], 201);
}
}
显示输出未知。
【问题讨论】:
-
你想在其他api中更新什么?你想怎么做?这些是可以导致解决的重要问题。如果您只想从其他 api 更新视频数据,您可以传递
uid,这是您在正文上的客户端响应,或者您可以将其放在 url(使用路由模型绑定的表单)。真的取决于,我认为你应该提供更多数据。 -
只需在您编写上传函数的同一个类中创建一个函数,更新函数的成功响应使用 $this->functionName($previousResponse); 调用该函数;
-
您将如何从另一个 API 调用此端点?其他 API 是否在不同的服务器上?
-
我已经更新了我的问题。如果您需要更多说明,请检查并告诉我
标签: laravel function api video upload