Skip to content

Commit b36394c

Browse files
author
Ville Jyrkkä
authored
Merge pull request #1173 from apinf/feature/add-api-logo
Feature/add api logo
2 parents 88795de + 0fd7b11 commit b36394c

File tree

16 files changed

+240
-0
lines changed

16 files changed

+240
-0
lines changed

apis/client/api_backends/edit/form/editApiBackendForm.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ <h1>{{_ "apiBackends_Edit_API_Title"}}</h1>
55
{{>showHelp 'api_name'}}
66
{{> afQuickField name='name'}}
77
<span class="help-block">{{_ "apiBackends_Title_Help"}}</span>
8+
<legend>Upload logo</legend>
9+
{{> uploadApiLogo apiBackend=apiBackend }}
810
<legend>{{_ "apiBackends_Backend_Title"}}</legend>
911
<span class="help-block">{{_ "apiBackends_Backend_Help"}}</span>
1012
{{> afQuickField name='backend_protocol' options="allowed"}}

apis/client/api_backends/view_api_backend/heading/heading.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
{{> apiBackendsUsageInstructionsButton }}
1010
</div>
1111
<h1 class="page-title">
12+
{{> viewApiLogo }}
1213
{{ apiBackend.name }}
1314
{{> viewApiBackendStatus apiBackend=apiBackend width="0.4" }}
1415
</h1>

apis/collection/backend.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ Schemas.ApiBackendsSchema = new SimpleSchema({
3333
type: String,
3434
optional: true
3535
},
36+
apiLogoFileId: {
37+
type: String,
38+
optional: true
39+
},
3640
documentation_link: {
3741
type: String,
3842
optional: true,

apis/logo/collection/collection.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const ApiLogo = new FileCollection('ApiLogo', {
2+
resumable: true, // Enable built-in resumable.js upload support
3+
http: [
4+
{ method: 'get',
5+
path: '/md5/:md5', // this will be at route "/gridfs/ApiLogos/:md5"
6+
lookup: function (params, query) { // uses express style url params
7+
return { md5: params.md5 }; // a query mapping url to ApiLogos
8+
}
9+
}
10+
]
11+
});
12+
13+
export { ApiLogo };
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { ApiLogo } from '/apis/logo/collection/collection';
2+
3+
ApiLogo.allow({
4+
insert: function(userId, file) {
5+
return Roles.userIsInRole(userId, ['admin','manager']);
6+
},
7+
remove: function(userId, file) {
8+
return Roles.userIsInRole(userId, ['admin','manager']);
9+
},
10+
read: function(userId, file) {
11+
return true;
12+
},
13+
write: function(userId, file, fields) {
14+
return true;
15+
}
16+
});

apis/logo/server/publications.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { ApiLogo } from '/apis/logo/collection/collection';
2+
3+
Meteor.publish('allApiLogo', function() {
4+
return ApiLogo.find({
5+
'metadata._Resumable': {
6+
$exists: false
7+
}
8+
});
9+
});
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { ApiLogo } from '/apis/logo/collection/collection';
2+
import { fileNameEndsWith } from '/lib/helperFunctions/fileNameEndsWith';
3+
4+
Meteor.startup( function() {
5+
ApiLogo.resumable.on('fileAdded', function(file) {
6+
return ApiLogo.insert({
7+
_id: file.uniqueIdentifier,
8+
filename: file.fileName,
9+
contentType: file.file.type
10+
}, function(err, apiLogoFile) {
11+
if (err) {
12+
console.warn("File creation failed!", err);
13+
return;
14+
}
15+
16+
const acceptedExtensions = ["jpg", "jpeg", "png", "gif"];
17+
18+
if (fileNameEndsWith(file.file.name, acceptedExtensions)) {
19+
20+
// Get the id from API logo file object
21+
const apiLogoFileId = file.uniqueIdentifier;
22+
23+
// Get apibackend id
24+
const apiBackend = Session.get('currentApiBackend');
25+
26+
// Update logo id field
27+
ApiBackends.update(apiBackend._id, {$set: { apiLogoFileId }});
28+
29+
sAlert.success('Logo successfully uploaded!');
30+
31+
return ApiLogo.resumable.upload();
32+
} else {
33+
34+
sAlert.error('Only .jpg, .jpeg, .png, .gif are accepted.');
35+
}
36+
37+
});
38+
});
39+
});

apis/logo/upload/client/upload.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.api-logo-file {
2+
margin-bottom: 1em;
3+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<template name="uploadApiLogo">
2+
<div class="api-logo-file">
3+
{{#unless uploadedApiLogoFile }}
4+
{{> uploadApiLogoButton }}
5+
{{else}}
6+
<ul class="list-group">
7+
<li class="list-group-item">
8+
<span class="glyphicon glyphicon-file"></span>
9+
{{ uploadedApiLogoFile.filename }}
10+
<span class="pull-right">
11+
<a class="btn btn-xs btn-danger delete-apiLogo">
12+
<span class="glyphicon glyphicon-trash"></span>
13+
</a>
14+
</span>
15+
</li>
16+
</ul>
17+
{{/unless}}
18+
</div>
19+
</template>

apis/logo/upload/client/upload.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import { ApiLogo } from '/apis/logo/collection/collection';
2+
3+
Template.uploadApiLogo.onCreated(function() {
4+
const instance = this;
5+
6+
// Subscribe to API logo
7+
instance.subscribe('allApiLogo');
8+
9+
instance.autorun(function () {
10+
const apiBackend = ApiBackends.findOne(instance.data.apiBackend._id);
11+
// Save apibackend id
12+
Session.set('currentApiBackend', apiBackend);
13+
});
14+
});
15+
16+
Template.uploadApiLogo.events({
17+
'click .delete-apiLogo': function(event, instance) {
18+
19+
// Show confirmation dialog to user
20+
const confirmation = confirm('Are you sure you want to delete this logo?');
21+
22+
// Check if user clicked "OK"
23+
if (confirmation === true) {
24+
25+
// Get currentApiBackend documentationFileId
26+
const apiLogoFileId = this.apiBackend.apiLogoFileId;
27+
28+
// Convert to Mongo ObjectID
29+
const objectId = new Mongo.Collection.ObjectID(apiLogoFileId);
30+
31+
// Remove API logo object
32+
ApiLogo.remove(objectId);
33+
34+
// Remove API logo file id field
35+
ApiBackends.update(instance.data.apiBackend._id, {$unset: { apiLogoFileId: "" }});
36+
37+
sAlert.success('Logo successfully deleted!');
38+
39+
}
40+
}
41+
});
42+
43+
Template.uploadApiLogo.helpers({
44+
uploadedLogoLink: function() {
45+
46+
const currentApiLogoFileId = this.apiBackend.apiLogoFileId;
47+
48+
// Convert to Mongo ObjectID
49+
const objectId = new Mongo.Collection.ObjectID(currentApiLogoFileId);
50+
51+
// Get API logo file Object
52+
const currentApiLogoFile = ApiLogo.findOne(objectId);
53+
54+
// Check if API logo file is available
55+
if (currentApiLogoFile) {
56+
// Get API logo file URL
57+
return Meteor.absoluteUrl().slice(0, -1) + ApiLogo.baseURL + "/md5/" + currentApiLogoFile.md5;
58+
}
59+
},
60+
uploadedApiLogoFile: function() {
61+
const currentApiBackend = Session.get('currentApiBackend');
62+
63+
const currentApiLogoFileId = currentApiBackend.apiLogoFileId;
64+
65+
// Convert to Mongo ObjectID
66+
const objectId = new Mongo.Collection.ObjectID(currentApiLogoFileId);
67+
68+
// Get API logo file Object
69+
const currentApiLogoFile = ApiLogo.findOne(objectId);
70+
71+
// Check if API logo file is available
72+
if (currentApiLogoFile) {
73+
return currentApiLogoFile;
74+
}
75+
}
76+
});

0 commit comments

Comments
 (0)