initial commit

This commit is contained in:
Dave Pedu 2016-06-02 19:58:03 -07:00
commit ccdf254be6
4 changed files with 188 additions and 0 deletions

82
pdffile.py Executable file
View File

@ -0,0 +1,82 @@
#!/usr/bin/env python
from PyPDF2 import PdfFileWriter, PdfFileReader
from reportlab.pdfgen import canvas
from StringIO import StringIO
# width 595 units
# height 841 units
def pdffile(imagelist, outputfilename, originalFileName, infoFile):
positions=[
[5,330],
[300, 330],
[5,5],
[300,5]
]
# number of QR codes
totalPieces = len(imagelist)
currentPiece=1
# PDF document is created here
pdf = PdfFileWriter()
while len(imagelist)>0:
# Get the first 4 images from the array
tmpImageList = []
while len(tmpImageList)<len(positions):
try:
tmpImageList.append(imagelist.pop(0))
except IndexError:
break
# Start making a page
imgTemp = StringIO()
imgDoc = canvas.Canvas(imgTemp)
# 4 qr's per page, so per each position
for pos in positions:
# Get the image name
try:
img = tmpImageList.pop(0)
except IndexError:
break
# Draw the QR
imgDoc.drawImage(img, 0+pos[0], 0+pos[1], 290, 290)
# Draw the label
imgDoc.drawString(0+pos[0],0+pos[1]+300, "%s :: %s of %s "%(originalFileName, currentPiece, totalPieces))
# Increment # of pieces drawn
currentPiece+=1
# Add logo
#imgDoc.drawImage('lib/qr_app_info.png', 5, 686, 150, 150)
# Add info code
imgDoc.drawImage(infoFile, 5, 676, 150, 150)
imgDoc.setFont("Helvetica-Bold", 12)
imgDoc.drawString(47,830, "SCAN ME")
# Add instructions
imgDoc.setFont("Helvetica-Bold", 18)
imgDoc.drawString(170,820, "What is this?")
imgDoc.drawString(170,754, "Instructions:")
imgDoc.setFont("Helvetica", 12)
imgDoc.drawString(170, 800, "The images below are pieces of a file encoded into QR codes scannable by")
imgDoc.drawString(170, 788, "smartphones. Scan the smaller QR code to the right for details about retrieving")
imgDoc.drawString(170, 776, "this file.")
imgDoc.drawString(170, 734, "1) Scan the image to the right and press Import.")
imgDoc.drawString(170, 722, "2) Scan in all available data QR codes.")
imgDoc.drawString(170, 710, "3) Download your file!")
# Save the page
imgDoc.save()
# Add page to master doc
page = PdfFileReader(StringIO(imgTemp.getvalue())).getPage(0)
pdf.addPage(page)
#break
# Export master doc
pdf.write(file(outputfilename,"w"))

47
qrfile.py Normal file
View File

@ -0,0 +1,47 @@
#!/usr/bin/env python
from qrcode import QRCode,constants
from base64 import b64encode
from zlib import compress
from urllib import quote as urlencode
from hashlib import md5
def qrstring(s):
qr = QRCode(error_correction=constants.ERROR_CORRECT_L, box_size=25,border=0)
qr.add_data(s)
qr.make()
return qr
def qrimage(s):
qr = qrstring(s)
return qr.make_image()
def qrimagefile(s, fileName):
qrimage(s).save(fileName, 'PNG')
return fileName
def md5_str(s):
x = md5()
x.update(s)
return x.hexdigest()
def qrfile(filepath, filehash, tmppath="tmp/"):
data = b64encode(compress(open(filepath, 'r').read(), 9))
template = 'http://qrfile.kilobyt.es/import/%s/%s/%s/?data=%s'
chunk = 0
files = []
while True:
datapiece = data[chunk*1024:chunk*1024+1024]
thisqr = template % (filehash[0:8], hex(chunk)[2:], md5_str(datapiece), urlencode(datapiece))
f = open('./urls.txt', 'a'); f.write(thisqr+"\n"); f.close()
img = qrimage(thisqr)
img.save(tmppath+"qr%s.png" % str(chunk), 'PNG')
files.append(tmppath+"qr%s.png" % str(chunk))
#print "Stored %s of %s" % (chunk*1024, len(data))
chunk+=1
if len(datapiece)<1024:
break
return files
if __name__ == "__main__":
from sys import argv
print qrfile(argv[1])

4
requirements.txt Normal file
View File

@ -0,0 +1,4 @@
PyPDF2==1.20
qrcode==4.0.4
six==1.10.0
wheel==0.24.0

55
run.py Executable file
View File

@ -0,0 +1,55 @@
#!/usr/bin/env python
from qrfile import qrfile,qrimagefile
from pdffile import pdffile
from sys import argv,exit
from os import unlink
import os.path
from json import dumps
from hashlib import md5
from datetime import datetime
from urllib import quote as urlencode
from os.path import getsize
if not len(argv)==3:
print "Specify an input file and output pdf like: ./run.py wallet.dat output.pdf"
exit(0)
# Calculate file hash
filehash = md5()
f = open(argv[1], 'r')
while True:
data = f.read(1024*1024)
if not data:
break
filehash.update(data)
filehash=filehash.hexdigest()
# create info-qr
fileInfo = {
"name":argv[1],
"hash":filehash,
"date":str(datetime.now()),
"size":getsize(argv[1]),
"pieces":0
}
# Covert file to many QR codes
files = qrfile(argv[1], fileInfo["hash"])
# Insert pieces info now that we have it
fileInfo["pieces"] = len(files)
# Create info QR code
infoUrl = 'http://qrfile.kilobyt.es/import/?data='+urlencode(dumps(fileInfo))
f = open('./urls.txt', 'a'); f.write(infoUrl+"\n\n\n"); f.close()
qrimagefile(infoUrl, 'tmp/info.png')
print infoUrl
# Embed QR codes indo PDF
# img list, output name, original file name, info qr
pdffile(files, argv[2], os.path.basename(argv[1]), 'tmp/info.png')
# Delete temp files
for item in files:
unlink(item)
unlink('tmp/info.png')