_binary From 10f64f02ef9ea3f9e9e74d0c40b8688e504d4706 Mon Sep 17 00:00:00 2001
From: pomishra13 <pooja192009@gmail.com>
Date: Wed, 13 May 2015 19:39:27 -0700
Subject: [PATCH] Mail Distribution code with video updater changes, a=pooja

---
 bin/media_updater.php               | 111 ++++++++++++++++++++++++++++++------
 controllers/crawl_controller.php    |  44 +++++++++++++-
 controllers/resource_controller.php |  32 ++++++++++-
 lib/media_constants.php             |  22 +++++++
 lib/utility.php                     |   9 +++
 models/source_model.php             |  13 +++++
 6 files changed, 209 insertions(+), 22 deletions(-)

diff --git a/bin/media_updater.php b/bin/media_updater.php
index 115f78d..37cbbbe 100644
--- a/bin/media_updater.php
+++ b/bin/media_updater.php
@@ -320,18 +320,18 @@ class MediaUpdater implements CrawlConstants,MediaConstants
         $video_paths = glob($convert_folder."/*");
         foreach ($video_paths as $video_path) {
             if (is_dir($video_path)){
-                if (!file_exists($video_path."/split.txt")) {return; }
-                if (file_exists($video_path."/split.txt")) {
+                if (!file_exists($video_path.self::SPLIT_FILE)) {return; }
+                if (file_exists($video_path.self::SPLIT_FILE)) {
                     crawlLog("Splitting the video $video_path");
-                    $lines = file($video_path."/file_info.txt");
+                    $lines = file($video_path.self::FILE_INFO);
                     $folder_name = rtrim($lines[1]);
                     $file_name = rtrim($lines[3]);
                     crawlLog("$folder_name : $file_name");
                     if ($folder_name && $file_name){
                         $this->splitVideo($folder_name, $file_name,
                             $video_path);
-                        unlink($video_path . "/split.txt");
-                        file_put_contents($video_path . "/count.txt",
+                        unlink($video_path . self::SPLIT_FILE);
+                        file_put_contents($video_path . self::COUNT_FILE,
                             count(glob($video_path . "/*.$type_string",
                                 GLOB_BRACE)));
                     }
@@ -355,7 +355,7 @@ class MediaUpdater implements CrawlConstants,MediaConstants
         $video_paths = glob($convert_folder."/*");
         foreach ($video_paths as $video_path) {
             crawlLog("  Video Path : $video_path");
-            $actual_count = file_get_contents($video_path."/count.txt");
+            $actual_count = file_get_contents($video_path.self::COUNT_FILE);
             crawlLog("  Actual_count : $actual_count");
             $timestamp_files = glob($video_path."/*.time.txt");
             $checked_out = count($timestamp_files);
@@ -367,10 +367,10 @@ class MediaUpdater implements CrawlConstants,MediaConstants
             crawlLog("  Converted count : $converted_count");
             if ($converted_count == $actual_count) {
                 crawlLog("  Conversion of segments complete!");
-                rename($video_path . "/count.txt",
-                    $converted_video_path . "/count.txt");
-                rename($video_path . "/file_info.txt",
-                    $converted_video_path . "/file_info.txt");
+                rename($video_path . self::COUNT_FILE,
+                    $converted_video_path . self::COUNT_FILE);
+                rename($video_path . self::FILE_INFO,
+                    $converted_video_path . self::FILE_INFO);
                 $this->sourceModel->db->unlinkRecursive($video_path);
             }
         }
@@ -388,20 +388,20 @@ class MediaUpdater implements CrawlConstants,MediaConstants
             mkdir($converted_folder);
         }
         foreach (glob($converted_folder."/*") as $video_path) {
-            if (file_exists($video_path . "/concatenated.txt")){ continue; }
-            if (file_exists($video_path . "/ready_to_assemble.txt")) { 
+            if (file_exists($video_path . self::CONCATENATED_FILE)){ continue; }
+            if (file_exists($video_path . self::ASSEMBLE_FILE)) { 
                 continue;
             }
-            if (!file_exists($video_path."/count.txt")) { continue; }
-            $actual_count = file_get_contents($video_path."/count.txt");
+            if (!file_exists($video_path.self::COUNT_FILE)) { continue; }
+            $actual_count = file_get_contents($video_path.self::COUNT_FILE);
             $video_segments = glob($video_path . "/*.{mp4}",GLOB_BRACE);
             $converted_count = count($video_segments);
             if ($actual_count == $converted_count) {
                 foreach($video_segments as $video_segment){
-                    file_put_contents($video_path . "/ready_to_assemble.txt",
+                    file_put_contents($video_path . self::ASSEMBLE_FILE,
                         "file "."'".(str_replace($video_path."/", "",
                         $video_segment))."'", FILE_APPEND);
-                    file_put_contents($video_path."/ready_to_assemble.txt",
+                    file_put_contents($video_path.self::ASSEMBLE_FILE,
                         PHP_EOL, FILE_APPEND);
                 }
             }
@@ -445,11 +445,11 @@ class MediaUpdater implements CrawlConstants,MediaConstants
         foreach (glob($converted_folder."/*") as $video_path) {
             crawlLog("  Video Path " . $video_path);
             if (is_dir($video_path)){
-                if(!file_exists($video_path . "/ready_to_assemble.txt")){
+                if(!file_exists($video_path . self::ASSEMBLE_FILE)){
                     continue;
                 }
-                $assemble_file = $video_path . "/ready_to_assemble.txt";
-                $lines = file($video_path . "/file_info.txt");
+                $assemble_file = $video_path . self::ASSEMBLE_FILE;
+                $lines = file($video_path . self::FILE_INFO);
                 $folder_name = trim($lines[1]);
                 $file_name = trim($lines[3]);
                 if($this->mergeVideo($assemble_file, $file_name, $folder_name)){
@@ -587,6 +587,79 @@ class MediaUpdater implements CrawlConstants,MediaConstants
             } 
         }
     }
+    /**
+     * Function to send emails to mailer batches created by 
+     * mail_server. This function would periodically be invoked and
+     * send emails reading data from the text files.
+     */
+    function sendGroupNotificationEmailsInBatches()
+    {
+        crawlLog("Checking for mailer files to be sent out...");
+        if ($current_machine == crawlHash(NAME_SERVER)) {
+            $mail_directory = WORK_DIRECTORY . self::MAIL_FOLDER;
+            if(!file_exists($mail_directory)){return;}
+            $files = glob($mail_directory."/*.{txt}",GLOB_BRACE);
+            $file_count=count($files);
+            if($file_count==0){
+                return;
+            }        
+            foreach($files as $file){
+                $fp = fopen($file, "a+");
+                if (flock($fp, LOCK_EX | LOCK_NB)){
+                    crawlLog("...Lock for".$file." acquired Sending emails\n");
+                    $lines = file($file);
+                    foreach($lines as $line){
+                        $mail_fields=explode("-",$line);
+                        $to = $mail_fields[0];
+                        $subject = $mail_fields[1];
+                        $message = $mail_fields[2];
+                        $header = $mail_fields[3];
+                        $mail_sent=mail($to, $subject, $message, $header);
+                    }
+                    flock($fp, LOCK_UN);
+                    unlink($file);
+                    sleep(MAIL_SENDING_INTERVAL);
+                }else{
+                    crawlLog("...Could not acquire the lock for ".$file!);
+                }
+            } 
+        } else {
+            $mail_directory = WORK_DIRECTORY "/schedules/mail";
+            $file_name = $this->sourceModel->requestFileForMailingList();
+            if(empty($file_name)){
+                crawlLog("...Could not get any response from name server!");
+                return;
+            }
+            $time = time();
+            $session = md5($time . AUTH_KEY);
+            $server = NAME_SERVER;
+            /* Download the file from the server */
+            $request = "$server?c=resource&a=get&time=$time&session=$session".
+            "&f=schedules&n=".urlencode($file_name)."&sf=mail";
+            $data = FetchUrl::getPage($request, NULL, true);
+            if (file_exists( $mail_directory)) {
+                $db->unlinkRecursive( $mail_directory);
+            }
+            mkdir($mail_directory);
+            $downloaded_file = $mail_directory . "/" . $file_name;
+            file_put_contents($downloaded_file, $data);
+            /* Send emails from the downloaded file */
+            $lines = file($downloaded_file);
+            foreach($lines as $line){
+                $mail_fields=explode("-",$line);
+                $to = $mail_fields[0];
+                $subject = $mail_fields[1];
+                $message = $mail_fields[2];
+                $header = $mail_fields[3];
+                $mail_sent = mail($to, $subject, $message, $header);
+                if ( $mail_sent == false ){
+                    crawlLog("...Error in sending email $to with $subject!");
+                }
+            }
+            $request = "$server?c=resource&a=unlockAndDeleteMailerFile.".
+            "&time=$time&session=$session&n=".urlencode($file_name);
+        }
+    }
 }
 /*
  * Instantiate and run the MediaUpdater program
diff --git a/controllers/crawl_controller.php b/controllers/crawl_controller.php
index e5200c1..1906fe0 100644
--- a/controllers/crawl_controller.php
+++ b/controllers/crawl_controller.php
@@ -61,7 +61,8 @@ class CrawlController extends Controller implements CrawlConstants,
         "combinedCrawlInfo", "getInfoTimestamp", "getCrawlItems",
         "getCrawlList", "getCrawlSeedInfo", "getNewsSources",
         "getSplitFileForConversion", "getUpdateProperties",
-        "sendStartCrawlMessage", "sendStopCrawlMessage", "setCrawlSeedInfo");
+        "sendStartCrawlMessage", "sendStopCrawlMessage", "setCrawlSeedInfo",
+        "getMailerListFileForSendingEmails");
     /**
      * Checks that the request seems to be coming from a legitimate fetcher then
      * determines which activity the fetcher is requesting and calls that
@@ -418,5 +419,46 @@ class CrawlController extends Controller implements CrawlConstants,
             echo webencode($folder_file);
         }
     }
+    /**
+     * Handles the request to get the mailer list file for
+     * sending emails. This selection is based upon if the file was taken
+     * previously or not. If it was then it is skipped.
+     * Otherwise new file is sent for sending emails and new text file 
+     * with taken prepended to the file name is generated.
+     */
+    function getMailerListFileForSendingEmails()
+    {
+        $mail_directory=WORK_DIRECTORY.self::MAIL_FOLDER;
+        if (!file_exists($mail_directory)){ return; }
+        $files = glob($mail_directory."/*.{txt}",GLOB_BRACE);
+        $file_count=count($files);
+        foreach($files as $file){
+            $file_name = str_replace($mail_directory."/","",$file);
+            if (strpos($file_name, 'taken') !== false){ 
+                continue;
+            } else {
+                $is_taken_file=$mail_directory."/taken-".$file_name;
+                if (in_array($is_taken_file, $files)){
+                    continue;
+                } else {
+                    $fp = fopen($mail_directory."/".$file_name, "a+");
+                    if (flock($fp, LOCK_EX | LOCK_NB)) {
+                        $taken_file_name="taken-".$file_name;
+                        file_put_contents($mail_directory."/".$taken_file_name,
+                             "working on");
+                        $mailer_file=$file_name;
+                    } else {
+                        continue;
+                    }
+                }
+            }
+            if (!empty($mailer_file)){
+                break;
+            } else {
+                continue;
+            }
+        }
+        echo webencode($mailer_file);
+    }
 }
 
diff --git a/controllers/resource_controller.php b/controllers/resource_controller.php
index 35ee668..950a8b0 100644
--- a/controllers/resource_controller.php
+++ b/controllers/resource_controller.php
@@ -51,7 +51,7 @@ class ResourceController extends Controller implements CrawlConstants,
      * @var array
      */
     var $activities = array("get", "syncList", "syncNotify", "suggest",
-        "uploadConvertedVideoFile");
+        "uploadConvertedVideoFile","unlockAndDeleteMailerFile");
     /**
      * Checks that the request seems to be coming from a legitimate fetcher
      * or mirror server then determines which activity  is being requested
@@ -112,7 +112,11 @@ class ResourceController extends Controller implements CrawlConstants,
                 return;
             }
             $folder = $_REQUEST['sf'];
-            $base_dir = CRAWL_DIR . self::CONVERTED_FOLDER . "/$folder";
+            if (strpos($folder, 'mail') !== false){
+                $base_dir = CRAWL_DIR . self::MAIL_FOLDER;
+            }else {
+                $base_dir = CRAWL_DIR . self::CONVERTED_FOLDER . "/$folder";;
+            }            
         } else {
             return;
         }
@@ -370,5 +374,29 @@ class ResourceController extends Controller implements CrawlConstants,
         echo $out . "Upload success!";
         return;
     }
+    /**
+     * Handles request to unlock the mailer list file
+     * and delete it.
+     * 
+     */
+    function unlockAndDeleteMailerFile()
+    {
+        if (!isset($_REQUEST['n'])){ return; }
+        $file_name = $_REQUEST['n'];
+        $mail_directory=WORK_DIRECTORY.self::MAIL_FOLDER;
+        if (!file_exists($mail_directory)){return;}
+        $files = glob($mail_directory."/*.{txt}",GLOB_BRACE);
+        $file_count = count($files);
+        foreach($files as $file){
+            if (strpos($file, $file_name) !== false){
+                $fp = fopen($file, "a+");
+                if (flock($fp, LOCK_EX | LOCK_NB)) {
+                    crawlLog("deleting file " $file!);
+                    flock($fp, LOCK_UN);
+                }
+                unlink($file);
+            }
+        }
+    }
 }
 
diff --git a/lib/media_constants.php b/lib/media_constants.php
index 895da54..c09d72d 100644
--- a/lib/media_constants.php
+++ b/lib/media_constants.php
@@ -48,6 +48,28 @@ interface MediaConstants
      * placing video files after conversion. 
      */
     const CONVERTED_FOLDER = "/schedules/media_converted";
+    /**
+     * The text file used to recognize the video file is 
+     * about to be split.
+     */
+    const SPLIT_FILE = "/split.txt";
+    /* The text file used to store the info of video file. */
+    const FILE_INFO = "/file_info.txt";
+    /**
+     * The text file used to store the count of split files
+     * generated from a video file. 
+     */
+    const COUNT_FILE = "/count.txt";
+    /**
+     * The text file used to store the list of split file
+     * names to concatenate them. 
+     */
+    const ASSEMBLE_FILE= "/ready_to_assemble.txt";
+    /**
+     * The text file used to recognize that the file has
+     * been concatenated. 
+     */
+    const CONCATENATED_FILE = "/concatenated.txt";
      /**
      * Used to place text files(mailer lists) for sending
      * in batches.
diff --git a/lib/utility.php b/lib/utility.php
index 0db8f9f..e80d272 100755
--- a/lib/utility.php
+++ b/lib/utility.php
@@ -1819,6 +1819,15 @@ function mimeType($file_name)
     if (substr($file_name, -4) == ".css") {
         $mime_type = "text/css";
     }
+    if(substr($file_name, -4) == ".mov"){
+        $mime_type = "video/quicktime";
+    }
+    if(substr($file_name, -4) == ".avi"){
+        $mime_type = "video/x-msvideo";
+    }
+    if(substr($file_name, -4) == ".mp4"){
+        $mime_type = "video/mp4";
+    }
     $mime_type = str_replace('application/ogg', 'video/ogg', $mime_type);
     return $mime_type;
 }
diff --git a/models/source_model.php b/models/source_model.php
index 984d399..97b9059 100644
--- a/models/source_model.php
+++ b/models/source_model.php
@@ -782,5 +782,18 @@ class SourceModel extends ParallelModel
         $value = webdecode($folder_file[0][CrawlConstants::PAGE]);
         return $value;
     }
+    /**
+     * Used to make an exec call to the crawl controller
+     * to get the next available file for sending emails
+     *
+     * @return string file name
+     */
+    function requestFileForMailingList()
+    {
+        $mailer_file = $this->execMachines("getMailerListFileForSendingEmails",
+            array(NAME_SERVER));
+        $file_name = webdecode($mailer_file[0][CrawlConstants::PAGE]);
+        return $file_name;
+    }
 }
 
-- 
1.9.4.msysgit.1

