Checking the progress of file download on the server side? - php

I am trying to build a service which allows to download music files. However after a user pays for a song, I want to be able to verify if the file was completely downloaded by the user or if the download failed in between. How can this be exercised? Thanks.

Theoretically you never know how many remaining packets the client will request. Therefore i see two options:
Dig deeeeep into your server code and come up with some hacky solution that somehow checks sent packets and tries to predict download completion
Write a native application that sends the progress back to the server (of course, then you will not have a web app any more)

As i understand user who pays for music have account on site. If he payed for music, add music to user's music list. And he will be able to download again in future. If download fail for any reason, user can re-download

Related

How to upload file through website, on mobile?

Is it possible to create a file/s upload system, on a mobile friendly website, that uploads the file from the mobile when it gets internet connection?
To put in a scenario: User X wants to upload 3 images on the website. X is outside, opens the website on this phone and selects the images he wants to upload. When he is about to start the upload, or has just started he looses internet connection. X then locks his phone, goes somewhere where there is internet and the file uploads continues and finishes. Is there a way to achieve something similar to this?
This is similar to Is there a browser file upload solution that can survive connection interruptions?
but the answers are over two years old. This question also doesn't talk about mobile, but I don't know if it matters with the file upload.
Sorry for the bad english and grammar
Regards
It is certainly possible. However I don't think a browser based solution will do. This has to do with both server and client side connections.
For example. In a traditional web server environment, the server will not keep a connection open when user looses its connectivity. When the user re-gains the connectivity the server may not know the state in which the client [or browser] is in ... so the client will need to communicate that state back to the server.
On the other hand the browser will also cease its connection irrespective of the fact whether its a mobile app or not.
I think you will need to supply an intelligent client , perhaps a Java based app to remember those issue and re-supply the broken session information to the server.
And just like that the server will need to be able to handle that .. though it can be an http server.

AJAX file upload with secret query vars

We're creating a form that allows users to upload large files. On mobile devices and slow connections, it might take a while to upload, so it seems important for this to be handled by an AJAX call that shows the users a progress bar (or something to let them know it's still working).
Here's the problem: The endpoint for the upload is a 3rd party API which expects our secret API key as one of the parameters. Here's a link directly to the section in their documentation. This API key cannot be exposed to the users on the client side.
My first instinct is to submit the form to an intermediate PHP script on our site, which has the API key, and then uploads the file to the API. But I'm pretty sure this will mean uploading the file twice: once to our server. Then again from our server to the API endpoint. Even if the form is submitted with AJAX, it's not a great result for the user to wait twice as long for it to complete.
So: What's the smoothest way to let users upload files while keeping our API key safe?
Some details that may or may not be important:
Our site is a PHP web app built on the CakePHP framework (v2.x). The files being uploaded are video files of all different formats between 1 and 5 minutes long. The API is a company called Wistia (see link to docs above). The file sizes seem to range from 3-30MB. We have no ability to change the way the 3rd party API works.
Uploading twice shouldn't be an issue - should it?
Its from your server to their API - this is what servers and APIs are meant for - exchanging data.
Javascript is not meant for this.
There is no way to hide it on the client, so your first instinct was correct - you will need to forward the file from the server.
It should be possible to read raw post stream from php://input, you can get the uploaded file from there (if you can parse it :)) and start upload to api server right away.
But even if the communication between mobile device and your script is slow, your script likely will likely upload fast to api server. So is it really needed?

How can I prevent users from downloading MP3s directly

I'm building a web-radio like service, in which the user authenticates to the services, gets a cookie and a Flash-based app plays mp3s from the server. The server only delivers if the client is allowed for that particular mp3.
If a user opens a HTTP logger (like FireBug), he can see the files being downloaded by flash. If he opens the mp3 URL directly via the address bar, he can easily download the MP3, although the URLs are not guessable by the user.
I'm looking for a safe system to prevent the user from downloading the MP3 directly to his system. I have examined last.fm, as they use a similar setup, and somehow they prevent it.
In the end, you're not going to be able to stop someone who's determined. However, you can at least make it difficult.
There are several options involving referrer checks, authentication, and fun stuff like that. But probably the most successful anti-downloading check I've seen was one that works like this:
The user indicates that he wants to stream a file; the app makes an authenticated, encrypted request indicating his desired action. The result is a one-use-only and time-limited URL that is recognized by whatever application or CDN is hosting the file. After the URL is used once (i.e. by the flash app) it then expires and can never be used again. If the streaming does not start within a given amount of time (several seconds), the URL likewise expires. Obviously the URL given does not directly correspond to the file name, but is instead authenticated, decoded, and translated server-side.
It's still not impossible to work around, but it's fairly difficult.
You might use RTMP instead of HTTP to deliver audio data. RTMP is meant to be used for streaming audio, video and misc data. It streams just data rather than a file. It's not 100% safe, because if something gets to client (browser, flash player, whatever), user can save it, but it's still better than giving a file via HTTP.
You will need a server that supports RTMP though, e.g., Flash Media Server (FMS), Wowza or Red5.

What is the concept behind Video Distribution Services?

I came across quite a few video distribution services (one is heyspread). The video they have on their home page is like, you upload your video only once and it is distributed to other video sharing sites. To me, it looks like they have coded it in Php.
Can anyone explain me the logic behind it? How is possible? Also, will it take the bandwidth of uploading it only once or uploading it to all the sites that we are using?
Also, if I had to code one like that, are there any links which I can use as a start up to kick off?
Here is my explanation based on what I know and your requirements:
Can anyone explain me the logic behind it?
You basically act as a mediator for all the sites. What the users do is given the site their permission to use their credentials that they set up in this multiple sites and allow you to upload video on their behalf so they don't have to do it themselves thereby saving them time
How is possible?
Many of the video hosting websites operate on HTTP protocol. In order to upload on your behalf the video distribution service do the following for each of the website (I have generalize the steps, there could be more than these steps for some of the sites):
Authenticate using the credentials that you give
Upload the video using one of the following ways:
If there is API available, then this is the preferred way for the service to upload on your behalf as the interface to authenticate/upload are clean and well define
If no API is available for a particular website, then the service has to simulate the HTTP sequence as if the request is made from the browser by the user. This is not the best way but sometimes is the only way. This approach is not as robust as the first one because the contract could change and you don't necessarily get the confirmation message (for success/failure of upload for example) other than via parsing the HTML
Also, will it take the bandwidth of uploading it only once or uploading it to all the sites that we are using?
It will use your bandwidth once to upload the video the first time. In order to upload to other websites on your behalf, the video distribution service will use its datacenter/cloud service bandwidth
Also, if I had to code one like that, are there any links which I can use as a start up to kick off?
I don't know any that does exactly like that, but you should look at each site that you want to mediate and see if they have the API first. You might want to start with those who have before trying to simulate user's click for those who don't have API. For example YouTube even goes further by providing the Client Library for you to use based on your language of choice.

How to record voice files using PHP, FLASH

I am developing an application where I need to record voice(for those who does not have a webcam so they can use only microphone to record voice) or video(those who have a a webcam - like laptop or external) files and save them on server, thereafter save the file name and logged in users id on database for later access.
I know I need to use some sort of flash app for this. But is there any free one which could server this purpose or even if there are paid ones which could serve this purpose that would be great. I tried google but may be I was not being able to go after the proper term or something, I could not succeed finding a proper solution for this.
Please, help me out!
You can do that simply using jRecorder, a jQuery plugin. You don't even need a Flash Media Server or RED server. JRecorder is same as jPlayer, you can use jQuery and HTML to design your recorder and you don't even need Flash or Action script knowledge for this.
jRecorder uses 1 pix hidden swf file which manages all the recording / previewing and sending the file data (wav file) to a URL you defined (Where you can write code in PHP or Java to receive a POST file)
It is quite simple and tidy.
You can download this Plugin from:
http://www.sajithmr.me/jrecorder-jquery
I have same feature in our latest project, the client want to have video recording from webcam and the video appear in the user's profile page.
For the server, we implement the RED5 server. It need a Java virtual machine in your hosting. You can read the detail requirement and installation instruction in the website.
To handle recording, we develop our own flash application, because the client request a custom interface to match with the overall website interface. I don't know the detail, since we outsource it to a fellow flash developer. Maybe you can see follow this thread, the development of flash recording by JeroenW.
To play recorded video, you can use any flash video player that support playing rtmp video source. You cannot play the recorded flv file in RED5 directly, since the file lack of metadata required by the player. Serving the recorded file as rtmp is done by RED5.
In addition to red5 there is Adobe's own Flash Media server that allows you to record audio/video straight to the server.
Or if you feel geeky you might be able to put together your own solution for this using a socket connection to the server and decoding the stream yourself on the server side. You should be able capture the audio/video locally into memory and then feed the byte stream up to your own server application.
There are Open Source solutions but you will need an own server to run them.
There is no way to run these things from shared hosting except if your provider is really nice, and ready to install the necessary software.
I asked the same question a few weeks back, check out the answers.
This question when googling "How to record audio php" comes up first so here for anyone from the future.
A simple way to record audio with flash and save it with PHP:
https://github.com/clouddueling/SimpleRecorder
Record audio, post audio data to your choice of url.
You could try recordmp3online.com which has an SDK. The nice thing about this one, is that it doesn't need a third party server(ala Red5), and supports mobile devices that don't have flash installed.

Categories