-
Notifications
You must be signed in to change notification settings - Fork 0
/
piDatabase.py
85 lines (66 loc) · 2.28 KB
/
piDatabase.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#Gabi Tessier, 7/19/2019
#This creates a database with MongoDB with PyMongo
import pymongo
from bson.objectid import ObjectId
import gridfs
from bson import objectid
#uploadphoto , downloadphoto
class RemSensDB():
db = None
fs = None
#creats the database
def DataBaseInitialize(self):
client = pymongo.MongoClient("mongodb://192.168.96.1:27017/database", username = "user1", password = "remote2019")
#Creating a client
self.db = client["database"]
#storing data
self.fs = gridfs.GridFS(self.db)
#inserts data into the database from the json file
#THIS IS NOT USED TO INSERT PHOTO'S - ONLY DATA****
def insertData(self, js):
self.db["raw_images"].insert_many(js)
#finds the object from a given id (i)
def findByID(self, i):
data = False
try:
ObjectId(i)
except Exception as e:
raise AssertionError("Invalid ID")
for grid_out in self.fs.find({"_id": ObjectId(i)}):
data = grid_out.read()
return data
#finds the object from a given name (n)
def findByName(self, n):
for grid_out in self.fs.find({"filename": n}):
data = grid_out.read()
return data
# store the data in the database. Returns the id of the file in gridFS
def uploadphoto(self, name):
b= open(name, 'rb')
store = self.fs.put(b, filename = name)
return store
# create an output file and store the image in the output file
def downloadphoto(self, a):
#retrieving data and returning .jpg in bytes
outputdata = self.fs.get(a).read()
return outputdata
def __init__(self):
self.DataBaseInitialize()
#self.insertData()
if __name__ == "__main__":
dbMan = RemSensDB()
#---------------TEST VARIABLES--------------------#
na = "image1"
id = "5d31ceeff814e0b3a9fe59de"
n = "image1.jpg"
filename = ""
#-------------------METHODS------------------------#
#++++++++++++DO NOT USE++++++++++++++++#
#dbMan.insertData(file)
#dbMan.queryDB(query)
#dbMan.findByDate(da)
#+++++++++++USE++++++++++++++++++++++++#
#print(dbMan.findByName(na))
#print(dbMan.findByID(id))
dbMan.uploadphoto(n)
#dbMan.downloadphoto(dbMan.uploadphoto(filename))