Skip to content
This repository has been archived by the owner on Aug 5, 2024. It is now read-only.

Commit

Permalink
modify image upload call to allow for standalone thumbnail upload
Browse files Browse the repository at this point in the history
  • Loading branch information
techcoderx committed Apr 10, 2019
1 parent 5e4d019 commit 832d34c
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 58 deletions.
2 changes: 1 addition & 1 deletion client/uploader.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ function uploadImage() {
updateProgressBar(progressPercent);
}
};
axios.post('/uploadArticleImg',imgFormData,contentType).then(function(response) {
axios.post('/uploadImage?type=images',imgFormData,contentType).then(function(response) {
console.log(response);
progressbar.style.display = "none";
document.getElementById('postBody').value += ('\n![' + document.getElementById('postImg').value.replace(/.*[\/\\]/, '') + '](https://cloudflare-ipfs.com/ipfs/' + response.data.imghash + ')');
Expand Down
108 changes: 51 additions & 57 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,8 @@ if (Config.UsageLogs == true) {
// Cache hashes data in a variable
var hashes = JSON.parse(fs.readFileSync('hashes.json','utf8'));

var uploadTokens = []

// Cache whitelist in a variable, and update variable when fs detects a file change
var whitelist = fs.readFileSync('whitelist.txt','utf8').split('\n')

fs.watchFile('whitelist.txt',() => {
fs.readFile('whitelist.txt', 'utf8',(err,readList) => {
if (err) return console.log('Error while updating whitelist: ' + err)
Expand Down Expand Up @@ -165,17 +163,6 @@ app.get('/auth',(request,response) => {
})
})

app.get('/uploadRequest',(request,response) => {
let username = request.query.user
if (!whitelist.includes(username)) return response.send({error: 'Looks like you do not have access to the uploader!'})
let generatedToken = username + '_' + Steem.formatter.createSuggestedPassword()
uploadTokens.push(generatedToken)
Steem.api.getAccounts([username],(err,res) => {
let encrypted_token = Steem.memo.encode(Keys.wifMessage,res[0].posting.key_auths[0][0],'#' + generatedToken)
response.send({encrypted_token: encrypted_token})
})
})

app.post('/uploadVideo', (request,response) => {
upload.fields([
{name: 'VideoUpload', maxCount: 1},
Expand Down Expand Up @@ -423,56 +410,63 @@ app.post('/uploadVideo', (request,response) => {
});
});

app.post('/uploadArticleImg',imgUpload.single('postImg'),(request,response) => {
let username = request.body.username; //steem username
let uploadedImg = request.file.filename;
fs.readFile('imguploads/' + uploadedImg,(err,data) => ipfsAPI.add(data,{trickle: true},(err,file) => {
if (Config.UsageLogs == true) {
// Log usage data for image uploads
if (usageData[username] == undefined) {
// New user?
usageData[username] = {};
}
app.post('/uploadImage',(request,response) => {
let imgType = request.query.type;
if (!imgType) return response.send({error: 'Image upload type not specified!'})
if (imgType != 'images' && imgType != 'thumbnails') return response.send({error: 'Invalid image upload type specified!'})

imgUpload.single('postImg')(request,response,(err) => {
if (err) return response.send({error: err})
let username = request.body.username; //steem username
let uploadedImg = request.file.filename;
fs.readFile('imguploads/' + uploadedImg,(err,data) => ipfsAPI.add(data,{trickle: true},(err,file) => {
if (Config.UsageLogs == true) {
// Log usage data for image uploads
if (usageData[username] == undefined) {
// New user?
usageData[username] = {};
}

var imgUsage = usageData[username]['images'];
if (imgUsage == undefined) {
usageData[username]['images'] = request.file.size;
} else {
usageData[username]['images'] = imgUsage + request.file.size;
var imgUsage = usageData[username][imgType];
if (imgUsage == undefined) {
usageData[username][imgType] = request.file.size;
} else {
usageData[username][imgType] = imgUsage + request.file.size;
}

fs.writeFile('usage.json',JSON.stringify(usageData),() => {});
}

fs.writeFile('usage.json',JSON.stringify(usageData),() => {});
}
// Log IPFS hashes by Steem account
if (hashes[username] == undefined) {
hashes[username] = {
videos: [],
thumbnails: [],
sprites: [],
images: [],
}
}

// Log IPFS hashes by Steem account
if (hashes[username] == undefined) {
hashes[username] = {
videos: [],
thumbnails: [],
sprites: [],
images: [],
// Patch for empty images array
if (hashes[username][imgType] == undefined) {
hashes[username][imgType] = [];
}
}

// Patch for empty images array
if (hashes[username].images == undefined) {
hashes[username].images = [];
}
// If hash is not in database, add the hash into database
if (!hashes[username][imgType].includes(file[0].hash))
hashes[username][imgType].push(file[0].hash);

fs.writeFile('hashes.json',JSON.stringify(hashes),(err) => {
if (err != null)
console.log('Error saving image hash logs: ' + err);
});

// If hash is not in database, add the hash into database
if (!hashes[username]['images'].includes(file[0].hash))
hashes[username]['images'].push(file[0].hash);

fs.writeFile('hashes.json',JSON.stringify(hashes),(err) => {
if (err != null)
console.log('Error saving image hash logs: ' + err);
});

// Send image IPFS hash back to client
response.send({
imghash: file[0].hash
})
}))
// Send image IPFS hash back to client
response.send({
imghash: file[0].hash
})
}))
})
})

app.get('/usage', CORS(), (request,response) => {
Expand Down

0 comments on commit 832d34c

Please sign in to comment.