WiP File upload started.
This commit is contained in:
@@ -1,24 +1,70 @@
|
|||||||
<br>
|
<br>
|
||||||
|
<div class="mdl-textfield mdl-js-textfield mdl-textfield--file">
|
||||||
|
<input type="file" id="selector">
|
||||||
|
</div>
|
||||||
|
|
||||||
<input type='file' />
|
<input class="mdl-button mdl-button--raised mdl-button--colored" type="button" value="Upload"
|
||||||
<br>
|
onclick="generateSignedURL()">
|
||||||
<img id="myImg" src="#" alt="your image" height=200 width=100>
|
|
||||||
|
|
||||||
<script>
|
<div class="mdl-textfield mdl-js-textfield mdl-textfield--file" id="status"></div>
|
||||||
|
|
||||||
function imageIsLoaded() {
|
<script type="text/javascript">
|
||||||
alert(this.src); // blob url
|
|
||||||
// update width and height ...
|
|
||||||
}
|
|
||||||
|
|
||||||
window.addEventListener('load', function() {
|
var c = "";
|
||||||
document.querySelector('input[type="file"]').addEventListener('change', function() {
|
var filename = "";
|
||||||
if (this.files && this.files[0]) {
|
|
||||||
var img = document.querySelector('img'); // $('img')[0]
|
function uuidv4() {
|
||||||
img.src = URL.createObjectURL(this.files[0]); // set src to blob url
|
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
|
||||||
img.onload = imageIsLoaded;
|
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
|
||||||
|
return v.toString(16);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function getFilename() {
|
||||||
|
var fullPath = document.getElementById('selector').value;
|
||||||
|
if (fullPath) {
|
||||||
|
var startIndex = (fullPath.indexOf('\\') >= 0 ? fullPath.lastIndexOf('\\') : fullPath.lastIndexOf('/'));
|
||||||
|
filename = fullPath.substring(startIndex);
|
||||||
|
if (filename.indexOf('\\') === 0 || filename.indexOf('/') === 0) {
|
||||||
|
filename = filename.substring(1);
|
||||||
|
}
|
||||||
|
filename = (uuidv4() + filename).replace(/\s+/g, '');
|
||||||
|
return (filename);
|
||||||
}
|
}
|
||||||
});
|
return (null);
|
||||||
});
|
}
|
||||||
|
|
||||||
|
function upload() {
|
||||||
|
var file = $('#selector')[0].files[0];
|
||||||
|
uploadFile(file)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
async function generateSignedURL() {
|
||||||
|
const file = getFilename();
|
||||||
|
const response = await fetch('/generateSignedURL?filename=' + file);
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error('Network response for fetch was not ok.');
|
||||||
|
}
|
||||||
|
c = await response.text();
|
||||||
|
c = c.replace(/\"/g, "")
|
||||||
|
console.log("Got signedURL: " + c)
|
||||||
|
console.log("Trying to upload " + file)
|
||||||
|
upload();
|
||||||
|
console.log("Complete")
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
function uploadFile(file) {
|
||||||
|
$("#status").html('Starting Upload...')
|
||||||
|
url = c
|
||||||
|
fetch(url, {
|
||||||
|
method: 'PUT',
|
||||||
|
body: file
|
||||||
|
})
|
||||||
|
.then(response => response.text())
|
||||||
|
.catch(error => $("#status").html(error)
|
||||||
|
)
|
||||||
|
.then(response => $("#status").html('File uploaded successfully: ' + filename));
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
<br>
|
<br>
|
||||||
<form id="publishForm" method="POST" novalidate enctype="multipart/form-data">
|
<form id="publishForm" method="POST" novalidate enctype="multipart/form-data">
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col s12">
|
<div class="col s18">
|
||||||
<ul class="tabs">
|
<ul class="tabs">
|
||||||
<li class="tab col s3"><a href="#publishBasicData">Osnovni podaci</a></li>
|
<li class="tab col s3"><a href="#publishBasicData">Osnovni podaci</a></li>
|
||||||
<li class="tab col s3"><a href="#publishAdditionalData">Dodatni podaci</a></li>
|
<li class="tab col s3"><a href="#publishAdditionalData">Dodatni podaci</a></li>
|
||||||
|
|||||||
64
index.js
64
index.js
@@ -56,27 +56,45 @@ setInterval(checkUpNotify, 1000 * 60 * 60 * 24);
|
|||||||
|
|
||||||
//Google storage req
|
//Google storage req
|
||||||
const storage = new Storage();
|
const storage = new Storage();
|
||||||
|
const BUCKET_NAME = "kivi_original_photos";
|
||||||
storage
|
const bucket = storage.bucket(BUCKET_NAME);
|
||||||
.getBuckets()
|
|
||||||
.then(results => {
|
|
||||||
const buckets = results[0];
|
|
||||||
|
|
||||||
console.log("Buckets:");
|
|
||||||
buckets.forEach(bucket => {
|
|
||||||
console.log(bucket.name);
|
|
||||||
});
|
|
||||||
})
|
|
||||||
.catch(err => {
|
|
||||||
console.error("GOOGLE ERROR:", err);
|
|
||||||
});
|
|
||||||
/*
|
/*
|
||||||
bucket = gcs.bucket('aBucket')
|
async function generateSignedUrl() {
|
||||||
bucket.file('aFile').getSignedUrl({
|
const options = {
|
||||||
action: 'write',
|
version: "v2",
|
||||||
expires: moment.utc().add(1, 'days').format(),
|
action: "write",
|
||||||
}, (error, signedUrl) => {
|
expires: Date.now() + 86400000
|
||||||
if (error == null) {
|
};
|
||||||
console.log(`Signed URL is ${signedUrl}`)
|
|
||||||
}
|
const [url] = bucket.file("aFile").getSignedUrl(options);
|
||||||
}) */
|
|
||||||
|
console.log(`The signed url for aFile is ${url}.`);
|
||||||
|
return url;
|
||||||
|
}*/
|
||||||
|
|
||||||
|
//generateSignedUrl().catch(console.error);
|
||||||
|
|
||||||
|
app.get("/generateSignedURL", (req, res) => {
|
||||||
|
console.log("Started server function!");
|
||||||
|
|
||||||
|
const options = {
|
||||||
|
version: "v2",
|
||||||
|
action: "write",
|
||||||
|
expires: Date.now() + 86400000
|
||||||
|
};
|
||||||
|
const filename = req.query.filename;
|
||||||
|
|
||||||
|
console.log("Filename: ", filename);
|
||||||
|
console.log("Bucket name:", bucket.name);
|
||||||
|
|
||||||
|
const url = bucket.file(filename).getSignedUrl(options, function(err, url) {
|
||||||
|
if (err) {
|
||||||
|
console.error(err);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
return url;
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log(`The signed url for ${filename} is ${url}.`);
|
||||||
|
res.send(url);
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user