1
0
mirror of https://github.com/Tygs/0bin.git synced 2023-08-10 21:13:00 +03:00

Merge branch 'v2' of github.com:Tygs/0bin into v2

This commit is contained in:
papee 2020-08-14 11:32:19 +02:00
commit 2b5dcc9ba9
6 changed files with 90 additions and 66 deletions

View File

@ -101,9 +101,9 @@ class Paste(object):
expiration = datetime.strptime(expiration, "%Y-%m-%d %H:%M:%S.%f") expiration = datetime.strptime(expiration, "%Y-%m-%d %H:%M:%S.%f")
except StopIteration: except StopIteration:
raise TypeError(to_ascii("File %s is malformed" % path)) raise TypeError("File %s is malformed" % path)
except (IOError, OSError): except (IOError, OSError):
raise ValueError(to_ascii("Can not open paste from file %s" % path)) raise ValueError("Can not open paste from file %s" % path)
return Paste(uuid=uuid, expiration=expiration, content=content) return Paste(uuid=uuid, expiration=expiration, content=content)
@ -228,3 +228,19 @@ class Paste(object):
Delete the paste file. Delete the paste file.
""" """
os.remove(self.path) os.remove(self.path)
@classmethod
def iter_all(cls, on_error=lambda e: e):
for p in settings.PASTE_FILES_ROOT.rglob("*"):
if p.is_file() and "counter" not in str(p):
try:
yield Paste.load_from_file(p)
except (TypeError, ValueError) as e:
on_error(e)
@property
def has_expired(self):
try:
return self.expiration < datetime.now()
except TypeError:
return False

View File

@ -176,7 +176,7 @@ input.hide-upload {
filter: alpha(opacity=0); filter: alpha(opacity=0);
opacity: 0; opacity: 0;
z-index: 2; z-index: 2;
width: 80px; width: 0;
margin-top: -20px; margin-top: -20px;
cursor: pointer; cursor: pointer;
cursor: hand; cursor: hand;
@ -230,6 +230,7 @@ li.L9 {
pre.prettyprint { pre.prettyprint {
width: 100%; width: 100%;
min-height: 100px; min-height: 100px;
padding: 5px;
} }
/* Specify class=linenums on a pre to get line numbering */ /* Specify class=linenums on a pre to get line numbering */

View File

@ -23,7 +23,6 @@ const app = new Vue({
el: '#app', el: '#app',
data: { data: {
previousPastes: [], previousPastes: [],
downloadLink: {},
displayBottomToolBar: false, displayBottomToolBar: false,
openPreviousPastesMenu: false, openPreviousPastesMenu: false,
readerMode: false, readerMode: false,
@ -32,7 +31,8 @@ const app = new Vue({
ownerKey: '', ownerKey: '',
id: '', id: '',
type: '', type: '',
content: '' content: '',
downloadLink: {},
}, },
newPaste: { newPaste: {
expiration: '1_day', expiration: '1_day',
@ -45,6 +45,8 @@ const app = new Vue({
clipboard: !!(isSecureContext && navigator.clipboard && navigator.clipboard.writeText), clipboard: !!(isSecureContext && navigator.clipboard && navigator.clipboard.writeText),
URLSearchParams: !!window.URLSearchParams,
localStorage: (function () { localStorage: (function () {
var val = !!(localStorage); var val = !!(localStorage);
document.querySelector('html').classList.add((val ? '' : 'no-') + 'local-storage'); document.querySelector('html').classList.add((val ? '' : 'no-') + 'local-storage');
@ -69,8 +71,20 @@ const app = new Vue({
methods: { methods: {
toggleReaderMode: function () { toggleReaderMode: function () {
debugger;
if (!this.readerMode) { if (!this.readerMode) {
this.messages = []; this.messages = [];
if (this.support.URLSearchParams) {
var searchParams = new URLSearchParams(window.location.search)
searchParams.set('readerMode', 1);
window.location.search = searchParams.toString();
}
} else {
if (this.support.URLSearchParams) {
var searchParams = new URLSearchParams(window.location.search);
searchParams.delete('readerMode');
window.location.search = searchParams.toString();
}
} }
this.readerMode = !this.readerMode; this.readerMode = !this.readerMode;
@ -387,6 +401,7 @@ window.zerobin = {
doneCallback(content); doneCallback(content);
} }
} catch (err) { } catch (err) {
debugger;
errorCallback(err); errorCallback(err);
} }
@ -628,23 +643,31 @@ window.zerobin = {
} }
} }
// return total * 250 / size;
return total * 1000 / size; return total * 1000 / size;
}, },
// prevent defaults
ignoreDrag: function (e) { ignoreDrag: function (e) {
e.stopPropagation(); e.stopPropagation();
e.preventDefault(); e.preventDefault();
}, },
// Handle Drop
handleDrop: function (e) { handleDrop: function (e) {
e.preventDefault(); e.preventDefault();
zerobin.upload(e.dataTransfer.files); zerobin.upload(e.dataTransfer.files);
document.getElementById("content").classList.remove("hover"); document.getElementById("content").classList.remove("hover");
}, },
handlePaste: function (e) {
var items = (event.clipboardData || event.originalEvent.clipboardData).items;
for (var i = 0; i < items.length; i++) {
if (items[i].type.indexOf("image") === 0) {
e.preventDefault()
return zerobin.upload([items[i].getAsFile()]);
}
}
},
handleDragOver: function (e) { handleDragOver: function (e) {
zerobin.ignoreDrag(e); zerobin.ignoreDrag(e);
document.getElementById("content").classList.add('hover'); document.getElementById("content").classList.add('hover');
@ -656,58 +679,29 @@ window.zerobin = {
upload: function (files) { upload: function (files) {
let content = document.getElementById('content'); let content = document.getElementById('content');
var current_file = files[0]; var currentFile = files[0];
var reader = new FileReader(); var reader = new FileReader();
if (current_file.type.indexOf('image') == 0) { if (currentFile.type.indexOf('image') == 0) {
reader.onload = function (event) { reader.onload = function (event) {
var image = new Image(); var image = new Image();
image.src = event.target.result; image.src = event.target.result;
content.value = event.target.result
image.onload = function () { image.onload = function () {
var maxWidth = 1024, var imgWrapper = document.createElement('div');
maxHeight = 1024, imgWrapper.classList.add('paste-wrapper');
imageWidth = image.width, imgWrapper.appendChild(image)
imageHeight = image.height;
if (imageWidth > imageHeight) {
if (imageWidth > maxWidth) {
imageHeight *= maxWidth / imageWidth;
imageWidth = maxWidth;
}
} else {
if (imageHeight > maxHeight) {
imageWidth *= maxHeight / imageHeight;
imageHeight = maxHeight;
}
}
var canvas = document.createElement('canvas');
canvas.width = imageWidth;
canvas.height = imageHeight;
image.width = imageWidth;
image.height = imageHeight;
var ctx = canvas.getContext("2d");
ctx.drawImage(this, 0, 0, imageWidth, imageHeight);
var paste = canvas.toDataURL(current_file.type);
content.value = paste;
content.dispatchEvent(new Event('change'));
image.style.maxWidth = '742px';
content.style.display = "none"; content.style.display = "none";
content.after(image); content.after(imgWrapper);
} }
} }
reader.readAsDataURL(current_file); reader.readAsDataURL(currentFile);
} else { } else {
reader.onload = function (event) { reader.onload = function (event) {
content.value = event.target.result content.value = event.target.result
content.dispatchEvent(new Event('change')); content.dispatchEvent(new Event('change'));
}; };
reader.readAsText(current_file); reader.readAsText(currentFile);
} }
} }
}; };
@ -755,9 +749,7 @@ if (content && key) {
/* When done */ /* When done */
function (content) { function (content) {
/* Decrypted content goes back to initial container*/ let readerMode = false;
document.querySelector('#paste-content').innerText = content;
app.currentPaste.content = content
if (content.indexOf('data:image') == 0) { if (content.indexOf('data:image') == 0) {
// Display Image // Display Image
@ -770,21 +762,34 @@ if (content && key) {
imgWrapper.classList.add('paste-wrapper'); imgWrapper.classList.add('paste-wrapper');
var img = document.createElement('img'); var img = document.createElement('img');
img.src = content; img.src = content;
//img.style.maxWidth = '742px';
pasteContent.after(imgWrapper); pasteContent.after(imgWrapper);
imgWrapper.appendChild(img); imgWrapper.appendChild(img);
// Display Download button
document.querySelectorAll('.btn-clone').forEach((node) => node.style.display = "none") document.querySelectorAll('.btn-clone').forEach((node) => node.style.display = "none")
app.downloadLink = { let extension = /data:image\/([^;]+);base64/.exec(content)[1];
name: '0bin_' + document.location.pathname.split('/').pop(),
app.currentPaste.downloadLink = {
name: '0bin_' + document.location.pathname.split('/').pop() + '.' + extension,
url: content url: content
} }
} else { } else {
app.currentPaste.type = "text" app.currentPaste.type = "text"
/* Decrypted content goes back to initial container*/
document.querySelector('#paste-content').innerText = content;
app.currentPaste.content = content
app.currentPaste.downloadLink = {
name: '0bin_' + document.location.pathname.split('/').pop() + ".txt",
url: "data:text/html;charset=UTF-8," + content
}
if (app.support.URLSearchParams) {
readerMode = (new URLSearchParams(window.location.search)).get('readerMode');
}
} }
bar.set('Code coloration...', '95%'); bar.set('Code coloration...', '95%');
@ -793,7 +798,7 @@ if (content && key) {
/** Syntaxic coloration */ /** Syntaxic coloration */
if (zerobin.isCode(content) > 100) { if (zerobin.isCode(content) > 100 && !readerMode) {
document.getElementById('paste-content').classList.add('linenums'); document.getElementById('paste-content').classList.add('linenums');
prettyPrint(); prettyPrint();
} else { } else {
@ -817,6 +822,9 @@ if (content && key) {
form.forEach((node) => node.disabled = false); form.forEach((node) => node.disabled = false);
content = ''; content = '';
if (readerMode) {
app.toggleReaderMode()
}
}, 100); }, 100);
@ -862,6 +870,7 @@ if (app.support.fileUpload) {
// Implements drag & drop upload // Implements drag & drop upload
let content = document.getElementById('content'); let content = document.getElementById('content');
content.addEventListener('drop', zerobin.handleDrop); content.addEventListener('drop', zerobin.handleDrop);
content.addEventListener('paste', zerobin.handlePaste);
content.addEventListener('dragover', zerobin.handleDragOver); content.addEventListener('dragover', zerobin.handleDragOver);
content.addEventListener('dragleave', zerobin.handleDragLeave); content.addEventListener('dragleave', zerobin.handleDragLeave);

View File

@ -60,10 +60,10 @@
<li class="submenu"><a href="#" @click.prevent="openPreviousPastesMenu = !openPreviousPastesMenu">Previous <li class="submenu"><a href="#" @click.prevent="openPreviousPastesMenu = !openPreviousPastesMenu">Previous
pastes v</a> pastes v</a>
<ul class="previous-pastes" id="topmenu" v-if="openPreviousPastesMenu"> <ul class="previous-pastes" id="topmenu" v-if="openPreviousPastesMenu">
<li class="item active" v-if="previousPastes.length === 0"> <li class="item" v-if="previousPastes.length === 0">
<a href="#">No paste yet</a> <a href="#">No paste yet</a>
</li> </li>
<li class="item active" v-for="paste in previousPastes"> <li :class="{item: true, active: paste.isCurrent}" v-for="paste in previousPastes">
<a :href="paste.link" @click="forceLoad(paste.link)">{% paste.displayDate %}</a> <a :href="paste.link" @click="forceLoad(paste.link)">{% paste.displayDate %}</a>
</li> </li>
</ul> </ul>

View File

@ -3,9 +3,10 @@
<div> <div>
<div class="file-upload" v-if="support.fileUpload"> <div class="file-upload" v-if="support.fileUpload">
<input type="button" class="btn btn-primary" :value="isUploading ? 'Uploading...': 'Upload file'" <label type="button" class="btn btn-primary"
:disabled="isUploading"> :disabled="isUploading">{% isUploading ? 'Uploading...': 'Upload file' %}
<input type="file" class="hide-upload" id="file-upload" @change="handleUpload($event.target.files)"> <input type="file" class="hide-upload" id="file-upload" @change="handleUpload($event.target.files)">
</label>
</div> </div>
</div> </div>

View File

@ -33,10 +33,8 @@
<div> <div>
<span class="paste-option btn-group"> <span class="paste-option btn-group">
<button class="btn btn-clone btn-secondary" @click.prevent="handleClone()">Clone</button> <button class="btn btn-clone btn-secondary" @click.prevent="handleClone()">Clone</button>
<button class="btn btn-secondary download-link" v-if="downloadLink.url"> <a class="btn btn-secondary download-link" :href="currentPaste.downloadLink.url"
<a :href="downloadLink.url" :download="downloadLink.name">Download</a> :download="currentPaste.downloadLink.name">Download</a>
</button>
<button class="btn btn-secondary">New Paste</button> <button class="btn btn-secondary">New Paste</button>
</span> </span>
</div> </div>
@ -74,9 +72,8 @@
<span class="paste-option btn-group"> <span class="paste-option btn-group">
<button class="btn btn-clone btn-secondary" @click.prevent="handleClone()">Clone</button> <button class="btn btn-clone btn-secondary" @click.prevent="handleClone()">Clone</button>
<button class="btn btn-secondary download-link" v-if="downloadLink.url"> <a class="btn btn-secondary download-link" :href="currentPaste.downloadLink.url"
<a :href="downloadLink.url" :download="downloadLink.name"> Download</a> :download="currentPaste.downloadLink.name"> Download</a>
</button>
<button class="btn btn-secondary">New Paste</button> <button class="btn btn-secondary">New Paste</button>
</span> </span>