B
B
BonBon Slick2021-04-20 23:31:48
linux
BonBon Slick, 2021-04-20 23:31:48

How to count the disk space used by the user on the server?

The task, the file store, is to bill the client for N used space.

The first options that come to mind
are 1 - to count by records in the database, indicating the number of bytes of each entity.
Disadvantages, the number of files on the server may differ from the records in the database and counting all entities is a very expensive operation
2 - put all the user's files in his personal folder on the server and simply reads its size
Disadvantages are the same as above. Therefore, it is necessary to check which files are there and delete which ones are not in the database, updating and giving the correct result.
3 - when uploading a file to the server by the user, increment the record in the database by the size of the file, in case of deleting the record from the database, the listener on the entity that deletes the file, and wrap everything in a transaction. Deleting a file after writing to the database, if the file is not deleted, exception and rollback.

Mmm, I think there are other options, quite late and already the brains do not boil which of the approaches to choose and why.
For example, to score on deletion synchronization, add crons that will run every week and clean orfans, guests, files without an owner.

Who will advise what?
Most likely there are already those who have implemented such a feature, how did they solve the problem of synchronizing the database of file records and the file system, the presence of files on the servers?

P.S. Linux added because maybe you need to do something like

exec('sudo du -shc /var/www/project_name/public/files/user_1/files/*')
it will depend on the approach.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2021-04-21
@BonBonSlick

Use disk quota.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question