How to limit Firebase Storage downloads to a certain maximum amount per user time period?

Asked

Viewed 40 times

1

I would like to know how I can limit the download demand on my app,20 per hour... obs. use firebase , no login, the only way I recognize a user and with id de anúncio do google the application only works with internet connection.

Updating
If the customer makes 10 downloads and comes back after 1 hour the ideal would be that Zera up the count.

This is the code I use to download:

 if (Environment.getExternalStorageState().equals(
                    Environment.MEDIA_MOUNTED)) {

                final String STORAGE = mChat = getIntent().getExtras().getString("storage");

                progress = ProgressDialog.show(MinhaActivity.this, "Download...",
                        "Aguarde...", true);
                FirebaseStorage mStorage = FirebaseStorage.getInstance();
                StorageReference storageRef = mStorage.getReferenceFromUrl("MY_DB").child(STORAGE).child(down+".mp3");

                File sdCardDirectory = Environment.getExternalStorageDirectory();
                final File localFile = new File(sdCardDirectory.getPath()+"/Audio",down+".mp3");


                storageRef.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
                    @Override
                    public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
                        Toast.makeText(Minhactivity.this,"Salvo em "+localFile.getAbsolutePath(),Toast.LENGTH_LONG).show();
                        progress.dismiss();

                    }
                }).addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception exception) {
                    }
                });


            }else{
                Toast.makeText(MinhaActivity.this,"Você está sem Cartão de memória",Toast.LENGTH_LONG).show();
            }

        }
    });

Thank you for your attention from now on!

1 answer

1


You can have a list of users (in your case, ad ids) and, associated with each user, a list of timestamps of the moments at which each download was made.

Before starting a download, take the last 20 and see if they are all within two hours (just subtract the most recent from the oldest and see if the interval is less than or equal to two hours). If they are, do not authorize the download. If they are not, add the current timestamp to the list and authorize the download.

  • I understood the logic expensive, more I have doubts, what is the result in timestamp for 1 hour... I had thought to use the timestamp more I’m not sure if it will always work the calculation.

  • I gave a studied here on timestamp all right now, thank you!

  • Not at all. I hope it works out there.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.