Skip to content
This repository has been archived by the owner on Mar 13, 2019. It is now read-only.

Commit

Permalink
Integrate CloudConvert RESTful API into app
Browse files Browse the repository at this point in the history
  • Loading branch information
josecolella committed Jun 30, 2014
1 parent 0a9a803 commit edfc73f
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 84 deletions.
2 changes: 1 addition & 1 deletion Final_Project/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
views.ExampleView.as_view(), name='example'),
url(r'^about/', views.AboutView.as_view(), name='about'),
url(r'^files/', views.fileview, name='file'),
url(r'^createSVG/', views.createSVGview, name='createcsv'),
url(r'^createSVG/(?P<filename>[a-zA-Z]+\d*\.[a-zA-Z]{1,4})', views.createSVGView, name='createsvg'),
url(r'^exportData/', views.exportDataView, name='exportData'),
url(r'^export/(?P<filename>[a-zA-Z]+\d*\.[a-zA-Z]{1,4})', views.exportView, name='export'),
url(r'^exportClear/', views.exportClearView, name='exportClear'),
Expand Down
1 change: 0 additions & 1 deletion visualization/static/visualization/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,6 @@ var saveFileAsPrompt = function(extension) {
contentClassName: 'alert-vex-content',
closeClassName: 'alert-vex-close',
callback: function(filename) {
console.log(filename);
var regex = /(\w+\d*)(\.[a-zA-Z]+)?/;
var checkFilename = regex.exec($.trim(filename));
var cleanfilename;
Expand Down
155 changes: 81 additions & 74 deletions visualization/static/visualization/js/exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,69 +17,27 @@ var exportExtensions = {
};

/**
* This method deals with the exportation of the SVG
* to the desired output format
*
* @param outputFormat The output format
* @return
*
* @param processUrl
* @param outputFormat
* @param fileUrl
* @param fileName
*/
var exportTo = function(outputFormat) {
var processUrl;

/**
* This returns the url that will be used to send the input file
* @param outputFormat
*/
var convertSVGToOutput = function(outputFormat) {

var startConversionProcess = function(processUrl, outputFormat, fileUrl, fileName) {

$.ajax({
url: 'https://api.cloudconvert.org/process',
type: 'POST',
dataType: '',
data: {
apikey: 't-APsbQ2IpfweLVeBQZgeZi4hEluptiRiJiImQuJuwiZ0ARQUIQ4hMCIDwSb8_Vg92Wp316XSdJDHUhIqzO1ug',
inputformat: "svg",
outputformat: outputFormat
},
success: function(data) {
processUrl = data.url;
convertProcess(outputFormat);
},
error: function() {
console.log("error");
}
})
.done(function() {
console.log("success");
})
.fail(function() {
console.log("error");
})
.always(function() {
console.log("complete");
});
};

var convertProcess = function(outputFormat) {
$.ajax({
url: 'https:'+processUrl,
type: 'POST',
dataType: 'json',
data: {
input: "download",
file: "http://cl.ly/0S3P0o3X192J/newfile.svg",
filename: "newfile.svg",
file: fileUrl,
filename: fileName,
outputformat: outputFormat
},
success: function(data) {
window.open(data.output.url);
},
error: function() {
console.log("error");
success: function(response) {
window.location = response.output.url;
}

})
.done(function() {
console.log("success");
Expand All @@ -90,35 +48,82 @@ var exportTo = function(outputFormat) {
.always(function() {
console.log("complete");
});
};

convertSVGToOutput(outputFormat);
};


/**
*
* @param outputFormat
* @param fileUrl
* @param filename
*/
var createProcessID = function(outputFormat, fileUrl, filename) {


$.ajax({
url: 'https://api.cloudconvert.org/process',
type: 'POST',
dataType: 'json',
data: {
apikey: 't-APsbQ2IpfweLVeBQZgeZi4hEluptiRiJiImQuJuwiZ0ARQUIQ4hMCIDwSb8_Vg92Wp316XSdJDHUhIqzO1ug',
inputformat: "svg",
outputformat: outputFormat
},
success: function(response) {
if (response.url !== undefined) {
var processUrl = response.url;
startConversionProcess(processUrl, outputFormat, fileUrl, filename);
}
},
error: function() {
console.log("error");
}
})
.done(function() {
console.log("success");
})
.fail(function() {
console.log("error");
})
.always(function() {
console.log("complete");
});
};



/**
* Inializes the current svg with namespaces and title attributes so
* that it can be sent to the server
* @param filename
* @returns {string|*}
*/
var initializeSVG = function(filename) {
var html = d3.select("svg")
.attr("title", $.trim(filename))
.attr("version", 1.1)
.attr("xmlns", "http://www.w3.org/2000/svg")
.node().parentNode.innerHTML.trim();

return html;
};
* Inializes the current svg with namespaces and title attributes so
* that it can be sent to the server
* @param filename
* @returns {string|*}
*/
var initializeSVG = function(filename) {
var html = d3.select("svg")
.attr("title", $.trim(filename))
.attr("version", 1.1)
.attr("xmlns", "http://www.w3.org/2000/svg")
.node()
.parentNode
.innerHTML
.trim();

return html;
};


/**
*
* @param filename
*/
var sendSVGInfo = function(filename) {


var svg = initializeSVG(filename);

$.ajax({
url: '/export/'+filename,
url: '/createSVG/'+filename,
type: 'POST',
dataType: 'JSON',
headers: {
Expand All @@ -129,7 +134,11 @@ var sendSVGInfo = function(filename) {
},
success: function(response) {
if (response.success === 1) {
window.location = response.url;
createProcessID(response['extension'],response['url'], response['filename']);
console.log(response['extension']);
console.log(response['filename']);
console.log(response['url']);

}

},
Expand All @@ -152,23 +161,21 @@ var sendSVGInfo = function(filename) {


var exportFile = {
name: undefined,
'pdf': function(filename) {

sendSVGInfo(filename);
},
'py': function(filename) {
window.location = '/export/'+filename
},
"png": function() {

sendSVGInfo(filename);
},
"svg": function(filename) {

var svg = btoa(initializeSVG(filename));
window.location = '/exportSVG/'+filename+'/'+svg+'/'
},
"jpg": function() {

sendSVGInfo(filename);
},
"csv": function() {

Expand Down
30 changes: 22 additions & 8 deletions visualization/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,9 @@ def exportDataView(request):
return HttpResponse(json.dumps(response_data), content_type="application/json")

def exportClearView(request):

"""
This views clears the information held in ExportUtils
"""
if request.is_ajax():
ExportUtils.clear()
if not ExportUtils.isValidExport():
Expand Down Expand Up @@ -291,13 +293,18 @@ def exportSVG(request, filename, svg):



def createSVGview(request):
def createSVGView(request, filename):
"""
This view receives the svg information from the workspace and saves the file
"""
if request.is_ajax():
newFile = ContentFile('newfile.svg', 'w')
newFile.name = 'newfile.svg'
print('here')
filenameRegex = re.search(r'(?P<filename>[a-zA-Z]+[\d\.]*)\.(?P<extension>[a-zA-Z]{1,4}$)', filename)
cleanFileName = filenameRegex.group('filename')
cleanFileExtension = filenameRegex.group('extension')
newFile = ContentFile(cleanFileName+'.svg', 'w')
newFile.name = cleanFileName+'.svg'


fileContent = base64.b64decode(request.POST['svg']).decode('utf-8')

Expand All @@ -310,7 +317,14 @@ def createSVGview(request):


# response_data = {'url': newFileDB.file.url, 'file': newFile}
# return HttpResponse(json.dumps(response_data), content_type="application/json")
response = HttpResponse(newFile, mimetype='image/svg+xml')
response['Content-Disposition'] = 'attachment; filename="newfile.svg"'
return response
response_data = {
'success': 1,
'url': newFileDB.file.url,
'filename': filename,
'extension': cleanFileExtension
}

return HttpResponse(json.dumps(response_data), content_type="application/json")
# response = HttpResponse(newFile, mimetype='image/svg+xml')
# response['Content-Disposition'] = 'attachment; filename="newfile.svg"'
# return response

0 comments on commit edfc73f

Please sign in to comment.