Skip to content

Commit

Permalink
Remove geolocation.io dependancy
Browse files Browse the repository at this point in the history
  • Loading branch information
thomashbrnrd committed Nov 7, 2023
1 parent 72f260b commit d59301e
Show file tree
Hide file tree
Showing 5 changed files with 2 additions and 41 deletions.
2 changes: 0 additions & 2 deletions backend/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,12 @@ async def imageupload(
background_tasks: BackgroundTasks,
image: UploadFile = File(...),
date: float = Form(...),
geolocation: str = Form(...),
user_id: Union[str, None] = Cookie(None),
):

# prepare content logs
user_agent = parse(request.headers.get("user-agent"))
extras_logging = get_base_logs(user_agent, user_id)
extras_logging["bg_geolocation"] = geolocation
extras_logging["bg_upload_time"] = round(time.time() - date, 2)

try:
Expand Down
16 changes: 1 addition & 15 deletions backend/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def test_upload(self):
r = client.post(
"/api/upload",
files={"image": f},
data={"date": time.time(), "geolocation": geoloc},
data={"date": time.time()},
)
assert r.status_code == 200
res = r.json()
Expand All @@ -94,7 +94,6 @@ def test_upload(self):
self.check_log_base(log)
assert log["short_message"] == "Identification request"
assert "-" in log["_bg_user_id"]
assert log["_bg_geolocation"] == geoloc
assert log["_bg_label"] == "revolver"
assert log["_bg_confidence"] == pytest.approx(98.43, 0.1)
assert log["_bg_upload_time"] >= 0
Expand Down Expand Up @@ -127,16 +126,3 @@ def test_feedback_and_logs(self):
assert log["_bg_confidence"] == confidence
assert log["_bg_label"] == label
assert log["_bg_confidence_level"] == confidence_level

def test_geoloc_api(self):
"""Checks that the geolocation api works properly"""
r = requests.get(
"https://api.ipgeolocation.io/ipgeo?apiKey=17dc6bed199b45ca92d60079686e03f1"
)
res = r.json()
assert "latitude" in res.keys()
assert "longitude" in res.keys()
lat = float(res["latitude"])
assert abs(lat) < 90
lon = float(res["longitude"])
assert abs(lon) < 180
15 changes: 1 addition & 14 deletions frontend/src/components/UploadButton.vue
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ async function submitUpload (base64: string, fileName: string) {
const fd = new FormData()
fd.append('image', file, file.name)
fd.append('date', '' + (Date.now() / 1000)) // date.now gives in milliseconds, convert to seconds
fd.append('geolocation', resultStore.geolocation)
try {
const { data } = await axios.post('/upload', fd)
Expand Down Expand Up @@ -110,19 +109,7 @@ async function srcToFile (src: string, fileName: string, mimeType: string) {
function onFileSelected (event: InputEvent & { target: InputEvent['target'] & { files: File[] } }) {
emit('file-selected', event)
const uploadedFile = event.target?.files[0]
// get user geolocation
axios.get('https://api.ipgeolocation.io/ipgeo?apiKey=17dc6bed199b45ca92d60079686e03f1', { withCredentials: false })
.then(res => {
const latitude = randomCoord(res.data.latitude as string)
const longitude = randomCoord(res.data.longitude as string)
resultStore.setGeolocation(latitude.toString() + ',' + longitude.toString())
})
.catch(() => {}) // TODO: Gérer l’erreur
.finally(() => {
// if geolocation is unavailable or incorrect format
resizeAndUpload(uploadedFile)
})
resizeAndUpload(uploadedFile)
}
</script>

Expand Down
9 changes: 0 additions & 9 deletions frontend/src/stores/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export const useResultStore = defineStore('result', () => {
const confidenceLevel = useLocalStorage<string>('confidenceLevel', null, { serializer })
const img = useLocalStorage<string>('img', null, { serializer })
const imgUrl = useLocalStorage<string>('imgUrl', null, { serializer })
const geolocation = useLocalStorage<string>('geolocation', null, { serializer })
const resultText = useLocalStorage<string>('resultText', null, { serializer })
const securingTutorial = useLocalStorage<boolean>('securingTutorial', false, { serializer })

Expand All @@ -22,7 +21,6 @@ export const useResultStore = defineStore('result', () => {
confidenceLevel?: string
img?: string
imgUrl?: string
geolocation?: string
resultText?: string
}

Expand All @@ -32,18 +30,13 @@ export const useResultStore = defineStore('result', () => {
confidenceLevel.value = result.confidenceLevel
img.value = result.img
imgUrl.value = result.imgUrl
geolocation.value = result.geolocation
resultText.value = result.resultText
}

const setSecuringTutorial = (newValue: boolean) => {
securingTutorial.value = newValue
}

const setGeolocation = (geoloc: string) => {
geolocation.value = geoloc
}

const updateTypology = (selectedOptionStep: TypologyKey) => {
const extra = (selectedOptionStep === 'revolver_black_powder' ? '_black_powder' : '')
typology.value = typology.value + extra as TypologyKey
Expand All @@ -56,11 +49,9 @@ export const useResultStore = defineStore('result', () => {
confidenceLevel,
img,
imgUrl,
geolocation,
resultText,
securingTutorial,
setResult,
setSecuringTutorial,
setGeolocation,
}
})
1 change: 0 additions & 1 deletion frontend/src/utils/storage-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export const clearLocalStorage: NavigationGuardWithThis<undefined> = (to, from,
confidenceLevel: undefined,
img: undefined,
imgUrl: undefined,
geolocation: undefined,
resultText: undefined,
})

Expand Down

0 comments on commit d59301e

Please sign in to comment.