简体   繁体   English

如何在云存储上检查 Firebase 的上传文件的文件类型?

[英]How to check the filetype of an uploaded file on Cloud Storage for Firebase?

I've got a simple file upload system in my app that uploads a .mp3 file to firebase.我的应用程序中有一个简单的文件上传系统,可以将.mp3文件上传到 firebase。 I would like to check to make sure the file being uploaded is definitely a .mp3 file and is no bigger than 80MB.我想检查以确保上传的文件绝对是.mp3文件并且不大于 80MB。 Is there a way to do this?有没有办法做到这一点?

Here is my code for uploading the files, at the moment:这是我目前上传文件的代码:

private void getMixForUpload() {
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("mp3/*");

    Intent chooser = Intent.createChooser(intent, "Choose Your Mix");
    startActivityForResult(chooser, ACTION_REQUEST_GALLERY);

}

@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    if (requestCode == ACTION_REQUEST_GALLERY && resultCode == RESULT_OK) {
        Uri uri = data.getData();

        StorageReference filepath = FirebaseStorage.getInstance().getReference().child("DjMixes").child(uri.getLastPathSegment());
        filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {


                taskSnapshot.getMetadata().getReference().getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                    @Override
                    public void onSuccess(Uri uri) {
                        Uri downloadUrl = uri;


                        firebaselogourl = downloadUrl.toString();

                    }
                });
            }

        }); 
}

So, basically, you have to use File Metadata .所以,基本上,你必须使用File Metadata Considering that you are getting the reference right and everything about the upload process is right, here is the way to do what you want:考虑到您获得了正确的参考并且有关上传过程的所有内容都是正确的,这是执行您想要的操作的方法:

taskSnapshot.getMetadata().addOnSuccessListener(new OnSuccessListener<StorageMetadata>() {
    @Override
    public void onSuccess(StorageMetadata storageMetadata) {
        long sizeMB = storageMetadata.getSizeBytes() / (1024 * 1024);
        String fileType = storageMetadata.getContentType();
    }
}).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception exception) {
        // Handle your error, something bad happened :( 
    }
});

Now that you have the size (in megabytes) and the content type you can just use a if statement like this:现在您有了大小(以兆字节为单位)和内容类型,您可以使用这样的if语句:

if (fileType.matches("audio/mpeg")  && sizeMB < 80)) {
   //your file is a audio file congratulations
   //your file also is bellow 80mb, good :)
} else {
   //the above statement is not true
}

As the OP pointed out it's much better to use the .matches() function, which, in this instance, is the only one that works.正如 OP 指出的那样,最好使用.matches() function,在这种情况下,它是唯一有效的。

NOTE: If you planning on deleting the file from firebase storage in case it's bigger than 80MB or it's not an mp3 file, I suggest that you do these things before uploading the files.注意:如果您打算从 firebase 存储中删除大于 80MB 或不是mp3文件的文件,我建议您在上传文件之前执行这些操作。 Basically, check if your file is mp3 and it's smaller than 80MB, then go upload it.基本上,检查您的文件是否为mp3且小于 80MB,然后 go 上传。 It improves the flow of the program and it's, frankly, a better way to do it anyways.它改善了程序的流程,坦率地说,无论如何,它是一种更好的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Java Spring 上的 firebase 存储中获取上传文件的下载 URL - How to get downloadURL of uploaded file in firebase storage on Java Spring Boot 使用 Java 将文件上传到 Cloud Storage for Firebase - Upload file to Cloud Storage for Firebase with Java 谷歌云存储MD5检查文件 - Google cloud storage md5 check on file 如何压缩图像并保存在firebase云存储中? - How to compress image and save in firebase cloud storage? 如何将上传到 Cloud Storage 的图片调整为指定大小? - How to resize images uploaded to Cloud Storage to a specified size? 如何获取本地开发服务器中Google云存储上载文件的公共链接(Google App Engine + JAVA) - How to get public link for the uploaded file on google cloud storage in local dev server(Google App engine+JAVA) 如何查看上传的文件是否为XLS文件? - How to check whether the uploaded file is XLS file or not? Firebase Cloud Storage Java Admin SDK 上传带有 contentType 的文件 - Firebase Cloud Storage Java Admin SDK upload file with contentType Firebase云存储中的Android访问文件仍处于离线状态 - Android access file in Firebase cloud storage whilst still offline 如何根据用户输入检查Amazon S3云存储中文本文件存储中的单词? - how to check for word in text file store in amazon s3 cloud storage against user input?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM