Skip to content

Commit

Permalink
BAE可用;准备修改项目名为Layer
Browse files Browse the repository at this point in the history
  • Loading branch information
oott123 committed Jul 17, 2013
1 parent 563e0ba commit 66bf115
Show file tree
Hide file tree
Showing 12 changed files with 2,411 additions and 29 deletions.
3 changes: 3 additions & 0 deletions bae.php
@@ -0,0 +1,3 @@
<?php
$_SERVER['PATH_INFO'] = $_SERVER['REQUEST_URI'];
include('./index.php');
8 changes: 6 additions & 2 deletions include/controller.php
Expand Up @@ -19,6 +19,7 @@ public function __construct($request = ''){
$this->succeed = TRUE;

$request = ltrim($request,'/');
$ext = 'cache';

//检测环境
if(!RUN_ENV){
Expand All @@ -35,6 +36,8 @@ public function __construct($request = ''){
else{
//检查防盗链
$referer = isset($_SERVER['HTTP_REFERER'])?$_SERVER['HTTP_REFERER']:'';
@$referer = parse_url($referer);
$referer = isset($referer['host'])?$referer['host']:'';
if(ALLOW_REGX && !preg_match('/'.ALLOW_REGX.'/i',$referer)){
$this->error_type = 'not_allowed_domain';
$this->succeed = FALSE;
Expand Down Expand Up @@ -87,7 +90,7 @@ public function __construct($request = ''){
$delete = true;
$request = $purge[1];
}
$key = md5($request).'_'.strlen($request).'.cache';
$key = (NO_KEY)?$request:md5($request).'_'.strlen($request).'.'.$ext;
$this->hit = $key;
$this->handle($request,$key,$delete,$direct);

Expand All @@ -108,7 +111,7 @@ private function handle($filename,$key,$delete = false,$direct = false){
die(json_encode(array('purge'=>$filename,'key'=>$key,'success'=>$return)));
}
if($storage->exists($key) && !$direct){
if($url = $storage->url($key)){
if(!NO_LOCATE && $url = $storage->url($key)){
$this->locate($url);
}
$content = $storage->read($key);
Expand Down Expand Up @@ -160,6 +163,7 @@ private function locate($url){
//302
header("HTTP/1.1 302 Moved Temporarily");
header("Location:".$url);
die();
}

/**
Expand Down
48 changes: 48 additions & 0 deletions include/storages/Bae.php
@@ -0,0 +1,48 @@
<?php

if ( ! defined('BASE_PATH')) exit('No direct script access allowed');

/**
* 封装SAE storage
* */
class StorageHandle{

public $instance;

public $domain;

public function __construct(){
$this->domain = DOMAIN;
require dirname(__FILE__).'/bcs/bcs.class.php';
$this->instance = new BaiduBCS();
}

public function exists($filename){
return $this->instance->is_object_exist($this->domain,$this->get_file($filename));
}
//这里是效率瓶颈啊!!
public function read($filename){
return $this->instance->get_object($this->domain,$this->get_file($filename));
}

public function write($name,$content){
return $this->instance->create_object_by_content($this->domain,$this->get_file($name),$content);
}

public function url($name){
//return $this->instance->getUrl($this->domain,$this->get_file($name));
return 'http://bcs.duapp.com/'.$this->domain.$this->get_file($name);
}

public function error(){
return false;
}

public function delete($name){
return $this->instance->delete_object($this->domain,$name);
}

private function get_file($name){
return '/'.ltrim($name,'/');
}
}
11 changes: 9 additions & 2 deletions include/storages/Local.php
Expand Up @@ -20,23 +20,30 @@ public function write($filename,$content){
return file_put_contents($this->get_file($filename),$content);
}
public function url($filename){
return false; //不提供URL方式读取
//return false; //不提供URL方式读取
return rtrim(DOMAIN,'/').'/'.$this->get_file($filename,false);
}
public function delete($filename){
return unlink($this->get_file($filename));
}
public function error(){
return false;
}
private function get_file($key){
private function get_file($key,$pre = true){
if(NO_KEY || NO_SECOND_FLODER){
if(!$pre) return $key;
return $this->data_dir.$key;
}
$letter1 = substr($key,0,1);
$letter2 = substr($key,0,2);
$dir = $this->data_dir.$letter1.'/'.$letter2;
if(!is_dir($dir)){
if(!mkdir($dir,0777,true)){
if(!$pre) return $key;
return $this->data_dir.$key;
}
}
if(!$pre) return $letter1.'/'.$letter2.'/'.$key;
return $dir.'/'.$key;
}
}

0 comments on commit 66bf115

Please sign in to comment.