Implementation of FlashPaper's Baidu Library imitation Function

Source: Internet
Author: User
Provides various official and user-released code examples. For code reference, you are welcome to exchange and learn which of the following experts have sent them to me. I forgot to share them with you. /*
Description: FlashPaper document conversion. Convert printable documents to FLASH format and save the converted information to the log_swfread table.

* Use FlashPaper service requirements
1. Run on Windows (xp/2003) Server
2. The system command exec can be called.
3. The called file directory must be readable and writable.
4. For the file type to be converted, the server must install the corresponding conversion software pdf installation pdf 9.0, office installation 2007, and must be genuine
5. The server must support the virtual printer function.
6. The server content should be large enough, with a minimum of 4 GB
7. Choose System Management> Site Management> set the website URL to be consistent with the access URL.

* Test FlashPaer
1. initialize the. bat file during initialization without any errors (xp/2003)
2. check whether there is a printer FlashPaer in the printer document.
3.run flashprinter.exe to drag the file to be converted. If the file can be converted, the software installation is normal.

* API call Method
1. synchronous call-supports simultaneous conversion of multiple files, and does not support execution time detection and conversion status detection
$ FlashPaper = A ('api. FlashPaper ');
$ FlashPaper-> convertFile ($ uploadlist );
2. asynchronous call-Multiple File Uploads need to be called multiple times, and the execution time detection and conversion status update are supported.
$ Url = $ this-> config ['site _ url']. 'api/FlashPaper/convertFile ';
Foreach ($ uploadList as $ data ){
Post ($ url, $ data );
}

* Note:
1. Simultaneous conversion of multiple files
-FlashPaper generates multiple independent processes. After the conversion is completed, the programs that are frequently and called are automatically closed (not supported yet)
2. Use pptx/docx/xlsx to convert data faster than 2003

* Maintain conversion information
-By default, the system saves the converted file name, file size, file type, reserved address after conversion, conversion time, conversion running time, and conversion failure error information.
-By default, the log_swfread database table is saved in execSaveInfo mode.

*/
Class FlashPaperAction extends Action
{
Public $ flashpaper_url = '/Public/FlashPaper/FlashPrinter.exe'; // Application Path
Public $ allow_file_ext = 'doc | docx | ppt | pptx | xls | xlsx | pdf | jpg | bmp | gif | png | txt '; // file format that can be converted (printable files)
Public $ swf_savepath = '/Data/swfscorce/'; // SWF Save the virtual path
Public $ time_limit = 1000; // Maximum program running time

Private $ socket = false; // whether to request a socket

Private $ scorce_file_path; // physical path of the uploaded file
Private $ scorce_save_path; // save path of the uploaded file
Private $ score_file_name; // name of the uploaded file
Private $ scorce_file_ext; // Upload File Extension
Private $ scorce_file_size; // File Size
Private $ exec_error; // file conversion error message
Private $ exec_start_time; // conversion Start Time
Private $ exec_end_time; // switch End Time
Private $ scorce_attch_id; // ID of the Attachment Information
Private $ swf_file_path = ''; // convert the swf file to save the physical path.


Public function test (){
// Test file conversion in various formats
// Supported files: txt pdf ppt pptx jpg gif png bmp xls
// Files being tested: docx, xlsx, and rtf pps pot doc
$ Command = 'd:/yjoa/Public/FlashPaper/FlashPrinter.exe-o D:/yjoa/Data/swfScorce/201110/1 .docx.swf D:/yjoa/Data/flashpaper/201110/1 .xlsx ';
$ Command = str_replace ("/", "\", $ command );

$ Exec_start_time = $ this-> exectime ();

Exec ($ command, $ return_array );
$ Exec_end_time = $ this-> exectime ();

Echo 'execue Time: '. round ($ exec_end_time-$ exec_start_time, 6 ).'
';
Echo $ command;
}

// Convert the DEMO page
Public function _ empty ()
{
$ Fconfig = F ('config ');

$ Upload_allow_ext = '';
$ Upload_allow_desc = '';
$ Upload_allow_extsarr = explode (',', $ fconfig ['upload _ allow_exts ']);

Foreach ($ upload_allow_extsarr as $ v)
{
$ Upload_allow_ext = $ upload_allow_ext. '*'. $ v .';';
$ Upload_allow_desc = $ upload_allow_desc. '.'. $ v .',';
}

$ Upload_allow_ext = substr ($ upload_allow_ext, 0,-1 );
$ Upload_allow_desc = substr ($ upload_allow_desc, 0,-1 );

$ This-> assign ('upload _ allow_desc ', $ upload_allow_desc );
$ This-> assign ('upload _ allow_ext ', $ upload_allow_ext );
$ This-> display ('read ');
}

/* Initialize */
Public function _ initialize ()
{
Ignore_user_abort (true );
Set_time_limit ($ this-> time_limit );

// Physical address of the FLASHPAPER Converter
$ This-> flashpaper_url =$ _ SERVER ['document _ root']. $ this-> flashpaper_url;

// Automatically create a conversion and save directory
@ Mkdir ('.'. $ this-> swf_savepath );
@ Mkdir ('.'. $ this-> swf_savepath.date ('ym '));

// Save the virtual path of the SWF File
$ This-> swf_file_path = $ this-> swf_savepath.date ('ym ');

// The physical path for saving the SWF File
$ This-> swf_savepath =$ _ SERVER ['document _ root']. $ this-> swf_savepath.date ('ym ').'/';
}

/* Call FlashPaper. $ uploadlist is an array of files uploaded by ThinkPHP UPLOAD */
Public function convertFile ($ uploadList)
{

If (! $ UploadList ){
$ UploadList = array (0 =>$ _ POST );
$ This-> socket = true;
}

If (is_array ($ uploadList ))
{
Foreach ($ uploadList as $ v)
{
$ This-> scorce_file_path = $ _ SERVER ['document _ root']. substr ($ v ['savepath'], 1). $ v ['savename'];
$ This-> score_file_name = strrpos ($ v ['savename'], '.')? Substr ($ v ['savename'], 0, strrpos ($ v ['savename'], '.'): $ v ['savename'];
$ This-> scorce_file_ext = $ v ['extension'];
$ This-> scorce_file_size = $ v ['SIZE'];
$ This-> scorce_attch_id = $ v ['attid']? $ V ['tid']: 0;
$ This-> scorce_save_path = substr ($ v ['savepath'], 1). $ v ['savename'];
$ This-> execFile ();
}
}

}

// Check whether the conditional conversion is met
Private function check ()
{
$ Err = 0;
$ This-> exec_error = '';
$ Err_tip = array (
1 => 'flashpaper can only run in Windows ',
2 => 'FlashPrinter.exe conversion file does not exist ',
3 => 'no exec function execution authorization ',
4 => 'flashprinter ('. $ this-> flashpaper_url.') Directory unreadable ',
5 => 'upload file ('. $ this-> scorce_file_path.') does not exist ',
6 => 'Save ('. $ this-> swf_savepath.') directory does not exist or cannot be writed ',
7 => 'conversion of the file format ('. $ this-> scorce_file_ext. ') is not supported ',
);

// Whether the WINDOWS environment is used
If (! IS_WIN ){
$ Err = 1;
}
// Check whether FlashPaper exists
Else if (! File_exists ($ this-> flashpaper_url )){
$ Err = 2;
}
// Check whether exec can be executed
Else if (! Function_exists ('exec ')){
$ Err = 3;
}
// Whether FlashPaper is readable
Else if (! Is_readable ($ this-> flashpaper_url )){
$ Err = 4;
}
// Whether the source file is readable
Else if (! Is_readable ($ this-> scorce_file_path )){
$ Err = 5;
}
// Save whether the directory is writable
Else if (is_dir ($ this-> swf_file_path )){
$ Err = 6;
}
// Check the file format
Else if (! In_array ($ this-> scorce_file_ext, explode ('|', $ this-> allow_file_ext ))){
$ Err = 7;
}

If ($ err ){
$ This-> exec_error = $ err_tip [$ err];
Return false;
}

Return true;

}

// Convert and upload files
Private function execFile ()
{
If ($ this-> check ())
{
$ Swf_path = $ this-> swf_savepath. $ this-> score_file_name.'.swf ';

// Relative path of the SWF File
$ This-> swf_file_path = $ this-> swf_file_path. '/'. $ this-> score_file_name.'.swf ';

// Convert the SWF command
$ Command = $ this-> flashpaper_url. '-O'. $ swf_path. $ this-> scorce_file_path;
$ Command = str_replace ("/", "\", $ command );

// Record conversion time
$ This-> exec_start_time = $ this-> exectime ();

// Call FlashPaper to execute the conversion
Exec ($ command );

$ This-> exec_end_time = $ this-> exectime ();

}

// Save the execution Information
$ This-> execSaveInfo ();

}

// Record the conversion operation data
Private function execSaveInfo ()
{
$ State = 0; // switch status, which is not converted by default
$ Time = time ();
$ Runtime = round ($ this-> exec_end_time-$ this-> exec_start_time, 6 );
$ Scorcepath = $ this-> scorce_file_path;

$ Swfread = M ('swfread ');

If ($ this-> exec_error = '')
{
$ SQL = "INSERT INTO 'Log _ swfread '(attid, filename, filesize, filetype, 'time', runtime, 'state', filepath, scorcepath)
VALUES
('{$ This-> scorce_attch_id }',
'". $ This-> score_file_name.'. '. $ this-> scorce_file_ext ."',
'{$ This-> scorce_file_size }',
'{$ This-> scorce_file_ext }',
'{$ Time }',
'{$ Runtime }',
'{$ State }',
'{$ This-> swf_file_path }',
'{$ This-> scorce_save_path }'
);";
}
Else
{
// Conversion failed
$ SQL = "INSERT INTO 'Log _ swfread '(attid, filename, filesize, filetype, 'time', runtime, 'state', filepath, scorcepath, 'error ')
VALUES
('{$ This-> scorce_attch_id }',
'". $ This-> score_file_name.'. '. $ this-> scorce_file_ext ."',
'{$ This-> scorce_file_size }',
'{$ This-> scorce_file_ext }',
'{$ Time }',
'{$ Runtime }',
'0 ',
'{$ This-> swf_file_path }',
'{$ This-> scorce_save_path }',
'{$ This-> exec_error }'
);";
}

$ Swfread-> execute ($ SQL );
$ Swfid = $ swfread-> query ("select max (swfid) as id FROM 'Log _ swfread '");

// Socket mode: Request listening status and execution time
If ($ this-> socket & $ this-> exec_error = ''){
$ I = 1;
While (true ){
If (file_exists ('.'. $ this-> swf_file_path )){
$ This-> exec_end_time = $ this-> exectime ();
$ Runtime = round ($ this-> exec_end_time-$ this-> exec_start_time, 6 );
$ SQL = "UPDATE log_swfread SET 'state' = 1, runtime = '{$ runtime}' WHERE swfid = '{$ swfid [0] ['id']}'";
$ Swfread-> query ($ SQL );
Break;
}
If (++ $ I >$ this-> time_limit-100) break;
Sleep (1 );
}
}
}

// Execution time
Private function exectime ()
{
$ Time = explode ("", microtime ());
$ Usec = (double) $ time [0];
$ Sec = (double) $ time [1];
Return $ sec + $ usec;
}
}
?>

AD: truly free, domain name + VM + enterprise mailbox = 0 RMB

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.