Skip to content

Instantly share code, notes, and snippets.

@robotron2084
Created July 7, 2016 02:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robotron2084/bdabcc5026f39350e188fa79c345c72d to your computer and use it in GitHub Desktop.
Save robotron2084/bdabcc5026f39350e188fa79c345c72d to your computer and use it in GitHub Desktop.
Cloudinary Form Data Issue
(function()
{
'use strict';
angular
.module('vid.vids.controllers')
.controller('NewVidController', NewVidController)
NewVidController.$inject = ['$rootScope', '$scope', 'Authentication', 'Snackbar', 'Vids', 'cloudinary'];
function NewVidController($rootScope, $scope, Authentication, Snackbar, Vids, cloudinary)
{
var vm = this;
vm.submit = submit;
$scope.files = {};
vm.files = $scope.files;
function submit(form)
{
if(form.$invalid || vm.video == undefined)
{
console.log("bad bad!");
return;
}
var duration = vm.start - vm.end;
// $scope.widget.options.duration = duration;
// $scope.widget.fileupload('option', 'formData').start = vm.start;
$scope.fileData.submit();
return;
// $rootScope.$broadcast('vid.created', {
// title: vm.title,
// author: {
// username: Authentication.getUserName()
// }
// });
// $scope.closeThisDialog();
// Vids.create(vm.title).then(createVidSuccessFn, createVidErrorFn);
// function createVidSuccessFn(data, status, headers, config)
// {
// Snackbar.show('Success! Vid created.');
// }
// function createVidErrorFn(data, status, headers, config)
// {
// $rootScope.$broadcast('vid.created.error');
// Snackbar.error(data.error);
// }
}
$scope.$on('ngDialog.opened', function (e, $dialog) {
var myVideo = document.getElementById('myvideo');
myVideo.addEventListener('loadeddata', function() {
$scope.vm.videoDuration = myVideo.duration;
$scope.vm.start = 0;
$scope.vm.end = Math.min($scope.vm.videoDuration, 10);
$scope.$apply();
}, false);
$scope.widget = $(".cloudinary_fileupload")
.unsigned_cloudinary_upload(cloudinary.config().upload_preset, {}, {
start: function (e) {
console.log("Hello from cloudinary upload");
$scope.status = "Starting upload...";
$scope.files = {};
// $scope.$apply();
},
fail: function (e, data) {
$scope.status = "Upload failed";
$scope.$apply();
}
})
.on("cloudinaryprogress", function (e, data) {
var name = data.files[0].name;
var file = $scope.files[name] || {};
file.progress = Math.round((data.loaded * 100.0) / data.total);
file.status = "Uploading... " + file.progress + "%";
$scope.files[name] = file;
$scope.$apply();
})
.on("cloudinaryprogressall", function (e, data) {
$scope.progress = Math.round((data.loaded * 100.0) / data.total);
$scope.status = "Uploading... " + $scope.progress + "%";
$scope.$apply();
})
.on("cloudinarydone", function (e, data) {
$rootScope.photos = $rootScope.photos || [];
data.result.context = {custom: {photo: $scope.title}};
$scope.result = data.result;
var name = data.files[0].name;
var file = $scope.files[name] ||{};
file.name = name;
file.result = data.result;
$scope.files[name] = file;
$rootScope.photos.push(data.result);
$scope.$apply();
}).on("cloudinaryfail", function(e, data){
var file = $scope.files[name] ||{};
file.name = name;
file.result = data.result;
$scope.files[name] = file;
});
$scope.widget.fileupload({
autoUpload : false
});
$scope.widget.bind('fileuploadadd', function (e, data) {
$scope.files = data.files;
$scope.fileData = data;
var file = data.files[0];
$scope.vm.videoFileUrl = URL.createObjectURL(file);
$scope.vm.videoSize = filesize(file.size);
$scope.vm.video = file;
$scope.vm.title = file.name;
$scope.$apply();
});
});
}
})();
<form role="form" class="form-horizontal" name="videoUploadForm" ng-submit="vm.submit(videoUploadForm)" ng-model="vm.files" novalidate>
<label>New Vid</label>
<div class="videoUploadContainer">
<input id="file" name="file" type="file" class="cloudinary_fileupload" accept="video/*" ng-hide="vm.videoFileUrl" ng-model="vm.video" />
<video id="myvideo" class="videoStuff" ng-show="vm.videoFileUrl" ng-src="{{ vm.videoFileUrl | trustUrl }}" controls ></video>
<div ng-show="vm.videoSize">{{ vm.videoSize }}</div>
<div ng-show="vm.videoSize">{{ status }}</div>
</div>
<div>
<div class="form-group" ng-show="vm.videoFileUrl">
<label for="vid__content" class="col-sm-2 control-label">Title</label>
<div class="col-sm-10">
<input class="form-control"
type="text"
id="vid__content"
name="videoTitle"
placeholder="Video Title"
ng-model="vm.title"
required
>
</input>
</div>
<!-- <span class="help-block" ng-show="videoUploadForm.videoTitle.$error.required">Required</span> -->
</div>
</div>
<div class="form-group" ng-show="vm.videoDuration">
<label class="col-sm-2 control-label">Time</label>
<div class="col-sm-10">
<div class="form-group row">
<label class="col-md-1 control-label" for="startField">Start</label>
<div class="col-md-2">
<input id="startField" class="form-control" type="text" ng-model="vm.start" required></input>
</div>
<label class="col-md-1 control-label" for="endField">End</label>
<div class="col-md-2">
<input id="endField" name="end" class="form-control" ng-show="vm.videoDuration" type="text" ng-model="vm.end" required></input>
</div>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-2">
</div>
<div class="col-sm-10">
<button ng-show="vm.videoFileUrl" type="submit" class="btn btn-primary">
Submit
</button>
</div>
</div>
<h2>Status</h2>
<div class="file" ng-repeat="file in vm.files">
<h3>{{file.name}}</h3>
<div class="status">{{file.status}}</div>
<div class="progress-bar">
<div class="progress" style="width: {{file.progress}}%" ng-init="progress=0"></div>
</div>
<div class="form_line">
<div class="form_controls">
<div class="preview">
<img ngf-src="file.result.url || file">
</div>
</div>
</div>
<div class="info">
<table>
<tr ng-repeat="(key, value) in file.result">
<td> {{key}} </td> <td> {{ value }} </td>
</tr>
</table>
</div>
</div>
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment