Unable to download Media file in Android app sent from web app - java

I am working on Chat Application, we have two Client one is Android and another is web,I am uploading Media files to S3-Amazon,when I am sending media file from Web App to android client Media file are not downloaded showing error as bellow.
Media Download interrupted : com.amazonaws.services.s3.model.AmazonS3Exception: The specified key does not exist. (Service: Amazon S3; Status Code: 404; Error Code: NoSuchKey; Request ID: XXXXXXXXX), S3 Extended Request ID:XXXXXXXXXXX
private void beginDownload(String key, String bucket, String
mediaType,final
DownloadFileFromAwsCompletionListener listener) {
// Location to download files from S3 to. You can choose any
accessible
// file.
String localFilePath = Strings.EMPTY;
try {
//if (!isThumb) {
localFilePath = MediaHelper.createMediaFile(mediaType, false, false, key);
/* } else {
localFilePath = MediaHelper.createMediaFile(mediaType, false, true);
}*/
} catch (Exception e) {
e.printStackTrace();
}
if (!StringHelper.isNullOrEmpty(localFilePath)) {
File file = new File(localFilePath);
// Initiate the download
TransferObserver observer = mTransferUtility.download(bucket, key, file);
final String finalLocalFilePath = localFilePath;
observer.setTransferListener(new TransferListener() {
#Override
public void onStateChanged(int id, TransferState state) {
//String bucketPath = UrlStrings.XmppStrings.
if (state.equals(TransferState.COMPLETED)) {
listener.onDownloadSuccess(finalLocalFilePath);
}
}
#Override
public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
}
#Override
public void onError(int id, Exception ex) {
listener.onDatabaseError(new AwsFailure(ex));
}
});
} else {
getLogger().log(Strings.TAG, "xmpp beginDownload(): file could not be created.");
}
}

You should cross check the uploaded path with which path you are using for downloading media files from S3. I think You are using the different path for downloading media files That's why you getting an error.

Related

Stream audio from aws s3 to Discord in java

I am trying to make a discord bot that plays custom sounds, i put the sounds in a aws s3 bucket and i can retrieve them but i dont know how to stream them to discord, i can stream audio files saved locally just fine, to stream local files i use lavaplayer.
This is how i get the file from the s3 bucket:
fullObject = s3Client.getObject(new GetObjectRequest("bucket-name", audioName));
System.out.println("Content-Type: " + fullObject.getObjectMetadata().getContentType());
S3ObjectInputStream s3is = fullObject.getObjectContent();
This i how i play the local files with lavaplayer:
String toPlay = "SoundBoard" + File.separator + event.getArgs();
MessageChannel channel = event.getChannel();
AudioChannel myChannel = event.getMember().getVoiceState().getChannel();
AudioManager audioManager = event.getGuild().getAudioManager();
AudioPlayerManager playerManager = new DefaultAudioPlayerManager();
AudioPlayer player = playerManager.createPlayer();
AudioPlayerSendHandler audioPlayerSendHandler = new AudioPlayerSendHandler(player);
audioManager.setSendingHandler(audioPlayerSendHandler);
audioManager.openAudioConnection(myChannel);
TrackScheduler trackScheduler = new TrackScheduler(player);
player.addListener(trackScheduler);
playerManager.registerSourceManager(new LocalAudioSourceManager());
playerManager.loadItem(toPlay, new AudioLoadResultHandler() {
#Override
public void trackLoaded(AudioTrack track) {
trackScheduler.addQueue(track);
}
#Override
public void noMatches() {
channel.sendMessage("audio not found").queue();
trackScheduler.addQueue(null);
}
#Override
public void loadFailed(FriendlyException throwable) {
System.out.println("error " + throwable.getMessage());
}
});
player.playTrack(trackScheduler.getTrack());
So is there a way to stream the files directly with lavaplayer or in another way? (im trying to avoid saving the audio to a file then playing it and then deleting it)

I can't upload files on remote server (Apache2) with Java Spring Boot and Angular 7. It works well on localhost

I can't upload files (images) on remote server(apache2). I use java spring boot for back and Angular 7 for front).
On localhost:4200 it works well. On a remote server I got from the chrome browser console :
POST http://www.xxx.tech:8081/images/upload 400
error: "{"timestamp":"2019-05-10T09:39:38.162+0000","status":400,"error":"Bad Request","message":"No file found","path":"/images/upload"}"
headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}
message: "Http failure response for http://www.xxx.tech:8081/images/upload: 400 OK"
name: "HttpErrorResponse"
ok: false
status: 400
statusText: "OK"
url: "http://www.xxx.tech:8081/images/upload"
The directory folder already exist on the VPS server.
How to make it work?
In my controller.java I tried to replace
File tmp = new File("../front/assets/img/").getCanonicalFile();
with
File tmp = new File("./front/assets/img/").getCanonicalFile();
and with
File tmp = new File("/home/alexandra/www/front/assets/img/").getCanonicalFile();
But it still show the same error message
JAVA :
ImageController.java
#PostMapping(value="images/upload")
public String uploadImage( #RequestParam("file") MultipartFile transferedFile) throws Exception{
try {
//FOR LOCALHOST (works)
//File tmp = new File("../FRONT-
//Alexandra/src/assets/img/").getCanonicalFile();
//FOR REMOTE SERVER (don't work)
File tmp = new File("../front/assets/img/").getCanonicalFile();
String destination = tmp.getPath() + "/" + transferedFile.getOriginalFilename();
File data = new File(destination);
transferedFile.transferTo(data);
Image image = new Image(transferedFile.getOriginalFilename(), destination);
imageRepository.save(image);
return destination;
}catch( Exception param_exception) {
throw new ResponseStatusException(
HttpStatus.BAD_REQUEST,
"No file found");
}
}
Angular :
mycomponent.component.ts
public apiUrl: string = environment.ApiUrl;
...
public uploadImaeg(): void {
this.imgesUploaded.map(image => {
if (image != null && image != undefined) {
this.imagesService.addImage(image).subscribe();
}
})
}
images.service.ts
public addImage(param_file: File): Observable<Object> {
const headers: HttpHeaders = new HttpHeaders();
const data: FormData = new FormData();
data.append("file", param_file, param_file.name);
headers.append("Content-Type", "multipart/form-data");
const Obs: Observable<boolean> = this.serviceHttp.post(
this.apiUrl + "images/upload", data, { headers: headers}
).pipe(
map(
(param_response: boolean) => {
return param_response;
}
)
);
return Obs;
}
environment.prod.ts
export const environment = {
production: true,
ApiUrl: 'http://'+document.location.hostname +':8081/'
};
I don't see you actually create a storage directory.
You might be want to add explicit directory creation due your bean initialization method something like:
private final Path rootLocation = Paths.get("upload");
#PostConstruct
public void init() {
try {
Files.createDirectories(rootLocation);
}
catch (IOException e) {
throw new StorageException("Could not initialize storage", e);
}
}
#AlexGera you made me understand what the problem was :
I tryed your code like this :
private final Path rootLocation = Paths.get("images/upload");
#PostConstruct
public void init() {
try {
Files.createDirectories(rootLocation);
}
catch (IOException e) {
throw new RuntimeException("Could not initialize storage", e);
}
}
#PostMapping(value="images/upload")
public String uploadImage( #RequestParam("file") MultipartFile transferedFile) throws Exception{
try {
File tmp = new File("/home/alexandra/www/front/assets/img/").getCanonicalFile();
String destination = tmp.getPath() + "/" + transferedFile.getOriginalFilename();
File data = new File(destination);
transferedFile.transferTo(data);
Image image = new Image(transferedFile.getOriginalFilename(), destination);
imageRepository.save(image);
return destination;
}catch( Exception param_exception) {
throw new ResponseStatusException(
HttpStatus.BAD_REQUEST,
"No file found");
}
}
it didn't resolve the problem but it created a folder in the back folder : image/upload. And, the rights of this folder was not root root like it is in all folders in filezilla server directory. It was alexandra alexandra
So, I changed the rights of the folder assets and the folder img (maybe only img, last folder, should work, I don't know) because my path is
/home/alexandra/www/front/assets/img/
I did that :
cd /home/alexandra/www/front
sudo chown alexandra:alexandra assets
cd assets
sudo chown alexandra:alexandra img
sudo service myserver stop
sudo service myserver start
sudo systemctl restart apache2
And it worked

FTPS storeFile return always false Java

im trying to send files to FTPS server
connection method: FTPS, ACTIVE, EXPLICIT
setFileType(FTP.BINARY_FILE_TYPE);
setFileTransferMode(FTP.BLOCK_TRANSFER_MODE);
Checking the reply string right after connect i got:
234 AUTH command ok. Expecting TLS Negotiation.
from here
234 Specifies that the server accepts the authentication mechanism specified by the client, and the exchange of security data is complete. A higher level nonstandard code created by Microsoft.
while trying to send file with storeFile or storeUniqeFile i get false
checking the reply string right after store file i got: 501 Server cannot accept argument.
what is weird i was able creating a directory to this client without any issues
with makeDirectory("test1");
i was trying both this links : link1 , link2
FOR EXAMPLE when i was trying to use ftp.enterLocalPassiveMode(); before ftp.storeFile(destinationfile, in);
i got time out error .
Does anyone have any idea how to solve it ?
public static void main(String[] args) throws Exception {
FTPSProvider ftps = new FTPSProvider();
String json = "connection details";
DeliveryDetailsFTPS details = gson.fromJson(json, DeliveryDetailsFTPS .class);
File file = File.createTempFile("test", ".txt");
FileUtils.write(file, " some test", true);
try (FileInputStream stream = new FileInputStream(file)) {
ftps.sendInternal(ftps.getClient(details), details, stream, file.getName());
}
}
protected void sendInternal(FTPClient client, DeliveryDetailsFTPS details, InputStream stream, String filename) throws Exception {
try {
// release the enc
DeliveryDetailsFTPS ftpDetails = (DeliveryDetailsFTPS) details;
setClient(client, ftpDetails);
boolean isSaved = false;
try (BufferedInputStream bis = new BufferedInputStream(stream)) {
isSaved = client.storeFile(filename, bis);
}
client.makeDirectory("test1");
client.logout();
if (!isSaved) {
throw new IOException("Unable to upload file to FTP");
}
} catch (Exception ex) {
LOG.debug("Unable to send to FTP", ex);
throw ex;
} finally {
client.disconnect();
}
}
#Override
protected FTPClient getClient(DeliveryDetails details) {
return new FTPSClient(isImplicitSSL((DeliveryDetailsFTPS ) details));
}
protected void setClient(FTPClient client, DeliveryDetailsFTPS details) throws Exception {
DeliveryDetailsFTPS ftpDetails = (DeliveryDetailsFTPS ) details;
client.setConnectTimeout(100000);
client.setDefaultTimeout(10000 * 60 * 2);
client.setControlKeepAliveReplyTimeout(300);
client.setControlKeepAliveTimeout(300);
client.setDataTimeout(15000);
client.connect(ftpDetails.host, ftpDetails.port);
client.setBufferSize(1024 * 1024);
client.login(ftpDetails.username, ftpDetails.getSensitiveData());
client.setControlEncoding("UTF-8");
int code = client.getReplyCode();
if (code == 530) {
throw new IOException(client.getReplyString());
}
// Set binary file transfer
client.setFileType(FTP.BINARY_FILE_TYPE);
client.setFileTransferMode(FTP.BLOCK_TRANSFER_MODE);
if (ftpDetails.ftpMode == FtpMode.PASSIVE) {
client.enterLocalPassiveMode();
}
client.changeWorkingDirectory(ftpDetails.path);
}
I have tried this solution as well didn't solve the problem:
they only way i was able send file is with FileZilla and it is using FTPES .
But i need my Java code to do it . can anyone give me a clue
I have tried almost any possible solution offered on different websites could not make it work with Apache FTPS CLIENT ,
had to use a different class which worked like a charm here is a snippet:
com.jscape.inet.ftps Link
private Ftps sendWithFtpsJSCAPE(ConnDetails details, InputStream stream, String filename) throws FtpException, IOException {
Ftps ftp;
FtpConnectionDetails ftpDetails = FtpConnectionDetails details;
ftp = new Ftps(ftpDetails.getHost(), ftpDetails.getUsername(), ftpDetails.getPassword());
if (ftpDetails.getSecurityMode().equals(FtpConnectionDetails.SecurityMode.EXPLICIT)) {
ftp.setConnectionType(Ftps.AUTH_TLS);
} else {
ftp.setConnectionType(Ftps.IMPLICIT_SSL);
}
ftp.setPort(ftpDetails.getPort());
if (!ftpDetails.getFtpMode().equals(FtpMode.ACTIVE)) {
ftp.setPassive(true);
}
ftp.setTimeout(FTPS_JSCAPE_TIME_OUT);
ftp.connect();
ftp.setBinary();
ftp.setDir(ftpDetails.getPath());
ftp.upload(stream, filename);
return ftp;
}

(Java) Download URL not working

I am battling with trying to download files using the google drive API. I'm just writing code that should download files from my drive onto my computer. I've finally got to a stage where I am authenticated and can view the file metadata. For some reason, I'm still unable to download files. The downLoadURL I get looks like:
https://doc-04-as-docs.googleusercontent.com/docs/securesc/XXXXXXXXXXXXXX/0B4dSSlLzQCbOXzAxNGxuRUhVNEE?e=download&gd=true
This URl isn't downloading anything when I run my code or when I copy and paste it in a browser. But, in the browser, when i remove the "&gd=true" part of the URL it downloads the file.
My download method is straight out of the google drive API documentation:
public static InputStream downloadFile(Drive service, File file) {
if (file.getDownloadUrl() != null && file.getDownloadUrl().length() > 0) {
try {
System.out.println("Downloading: "+ file.getTitle());
return service.files().get(file.getId()).executeMediaAsInputStream();
} catch (IOException e) {
// An error occurred.
e.printStackTrace();
return null;
}
} else {
// The file doesn't have any content stored on Drive.
return null;
}
}
Anyone know whats going on here?
Thanks in advance.
Since you're using Drive v2, a different approach (also on the documentation) is for you to get the InputStream thru the HttpRequest object.
/**
* Download a file's content.
*
* #param service Drive API service instance.
* #param file Drive File instance.
* #return InputStream containing the file's content if successful,
* {#code null} otherwise.
*/
private static InputStream downloadFile(Drive service, File file) {
if (file.getDownloadUrl() != null && file.getDownloadUrl().length() > 0) {
try {
HttpResponse resp =
service.getRequestFactory().buildGetRequest(new GenericUrl(file.getDownloadUrl()))
.execute();
return resp.getContent();
} catch (IOException e) {
// An error occurred.
e.printStackTrace();
return null;
}
} else {
// The file doesn't have any content stored on Drive.
return null;
}
}

how to send a Photo with caption in telegram using ex3ndr?

I am using ex3ndr for creating a telegram client. now i want to send a message witch has a photo and a caption or description. I send photo using this code snippet:
private static void sendMedia(PeerState peerState, String fileName) {
TLAbsInputPeer inputPeer = peerState.isUser() ? new TLInputPeerContact(peerState.getId()) : new TLInputPeerChat(peerState.getId());
int task = api.getUploader().requestTask(fileName, null);
api.getUploader().waitForTask(task);
int resultState = api.getUploader().getTaskState(task);
Uploader.UploadResult result = api.getUploader().getUploadResult(task);
TLAbsInputFile inputFile;
if (result.isUsedBigFile()) {
inputFile = new TLInputFileBig(result.getFileId(), result.getPartsCount(), "file.jpg");
} else {
inputFile = new TLInputFile(result.getFileId(), result.getPartsCount(), "file.jpg", result.getHash());
}
try {
TLAbsStatedMessage res = api.doRpcCall(new TLRequestMessagesSendMedia(inputPeer, new TLInputMediaUploadedPhoto(inputFile), rnd.nextInt()), 30000);
res.toString();
} catch (IOException e) {
e.printStackTrace();
}
}
but I donot know how can add caption to this photo?(this code snippet is a sample from this url: ex3ndr sample
)
ex3ndr library only support layer 12 of Telegram API where sendMedia method doesn't support captions in photos. That's means this library is not able to send captions with photos, the layer should be updated before being able of doing so (and the repository seems to be abandoned).

Categories