Check if file ID exists before inserting into DB

File IDs collisions are improbable but possible. This check avoids this from happening.
Merge pull request #3133 from RodrigoBalest/2.2/check-file-id-before-insert

Check if file ID exists before inserting into DB