蝉知门户系统迁移到SAE平台-File模块扩展

    安装完成后虽然可以正常浏览网站了,但是由于upload目录没有写权限,还需要对文件管理模块进行修改以适应sae的环境,经过导师指点查看了禅道sae3.0版本的迁移方案,初步确定修改思路。也使用了部分原迁移方案的代码,感谢禅道3.0sae版本的开发者留下的资料与经验。

1.文件的保存。需要将文件保存到storage上。

2.文件获取(路径)。需要将路径更改为storage路径。

3.文件管理(在上传后有图片空间对话框进行文件的查看)

3.1目录列表获取

3.2文件移动

3.3文件删除

    (一)在修改过程中使用最新的saestorage.php时由于没有找到完整的phpAPI包,引入swift.php时报错。所以暂时使用了禅道中使用的saestorage.php文件。第一步先仿照禅道的迁移方案进行了扩展使文件上传实现。增加file/ext/model/sae.php文件(修改保存文件方式,修改文件路径获取)

<?php
public function getById($fileID)
{
    $file = $this->dao->findById($fileID)->from(TABLE_FILE)->fetch();
    $file->realPath = 'saestor://' . $this->config->sae->storage->domain . '/' . $file->pathname;
    $file->webPath  = $this->app->loadClass('saestorage')->getURL($this->config->sae->storage->domain, $file->pathname);
    return $file;
}

public function saveUpload($objectType = '', $objectID = '', $extra = '')
{
    $fileTitles = array();
    $now        = helper::now(); //原使用today函数sql插入时不成功
    $files      = $this->getUpload();

    $this->storage = $this->app->loadClass('saestorage');
    foreach($files as $id => $file)
    {
        $this->storage->upload($this->config->sae->storage->domain, $file['pathname'], $file['tmpname']);
        $file['objectType'] = $objectType;
        $file['objectID']   = $objectID;
        $file['addedBy']    = $this->app->user->account;
        $file['addedDate']  = $now;
        $file['extra']      = $extra;
        unset($file['tmpname']);
        $this->dao->insert(TABLE_FILE)->data($file)->exec();
        $fileTitles[$this->dao->lastInsertId()] = $file['title'];
    }
    return $fileTitles;
}
增加file/ext/control/ajaxupload.php(将文件保存到SAE storage)
<?php
class file extends control
{
    public function ajaxUpload()
    {
        $file = $this->file->getUpload('imgFile');
        $file = $file[0];
        if($file)
        {
            $url = $this->app->loadClass('saestorage')->upload($this->config->sae->storage->domain, $file['pathname'], $file['tmpname']);

            $file['addedBy']    = $this->app->user->account;
            $file['addedDate']  = helper::today();
            unset($file['tmpname']);
            $this->dao->insert(TABLE_FILE)->data($file)->exec();

            die(json_encode(array('error' => 0, 'url' => $url)));
        }
    }
}
增加file/ext/control/buildform.php(去掉目录是否可写的检查)
<?php
class file extends control
{
    /**
     * Build the upload form.
     * 
     * @param  int    $fileCount 
     * @param  float  $percent 
     * @access public
     * @return void
     */
    public function buildForm($fileCount = 2, $percent = 0.9)
    { 
        $this->view->fileCount = $fileCount;
        $this->view->percent   = $percent;
        $this->display();
    }
}
以上三段代码来自禅道SAE3.0版本。

    经以上扩展后能实现基本的文件上传。但是涉及到文件管理的部分扔有问题。下一步就是添加扩展使文件管理相关的功能实现。

   (二)修改文章附件相关

    在以上修改后打开附件发现提示目录不可写,添加model的checkSavePath()方法扩展后,发现上传文件选择form没有出现,经过调试排查发现是browse.html.php里的<td><?php echo $this->fetch('file', 'buildForm');?></td>执行fetch方法时未返回内容,怀疑是添加buildForm方法扩展导致,遂删除buildForm扩展。解决!

    修改内容如下:在上一步中添加的file/ext/model/sae.php文件中添加checkSavePath()方法扩展,去除目录是否可写的检查,添加后文件:

<?php
public function getById($fileID)
{
    $file = $this->dao->findById($fileID)->from(TABLE_FILE)->fetch();
    $file->realPath = 'saestor://' . $this->config->sae->storage->domain . '/' . $file->pathname;
    $file->webPath  = $this->app->loadClass('saestorage')->getURL($this->config->sae->storage->domain, $file->pathname);
    return $file;
}

public function saveUpload($objectType = '', $objectID = '', $extra = '')
{
    $fileTitles = array();
    $now        = helper::now(); //原使用today函数sql插入时不成功
    $files      = $this->getUpload();

    $this->storage = $this->app->loadClass('saestorage');
    foreach($files as $id => $file)
    {
        $this->storage->upload($this->config->sae->storage->domain, $file['pathname'], $file['tmpname']);
        $file['objectType'] = $objectType;
        $file['objectID']   = $objectID;
        $file['addedBy']    = $this->app->user->account;
        $file['addedDate']  = $now;
        $file['extra']      = $extra;
        unset($file['tmpname']);
        $this->dao->insert(TABLE_FILE)->data($file)->exec();
        $fileTitles[$this->dao->lastInsertId()] = $file['title'];
    }
    return $fileTitles;
}

public function checkSavePath()
{
    return true;
}
将上一步中的file/ext/control/buildform.php删除。

附件显示的时候会间接调用processFile($file)方法,该方法会返回file的fullURL,需要将该url修改为storage的,在file/ext/model/sae.php文件中扩展该方法(修改的$file->webPath和$filr->fullURL两行)

public function processFile($file)
{
    $file->webPath   = $this->app->loadClass('saestorage')->getURL($this->config->sae->storage->domain, $file->pathname);
    $file->fullURL   = $file->webPath;
    $file->middleURL = '';
    $file->smallURL  = '';
    $file->isImage   = false;
    
    if(in_array(strtolower($file->extension), $this->config->file->imageExtensions) !== false)
    {
        $file->middleURL = $this->webPath . str_replace('f_', 'm_', $file->pathname);
        $file->smallURL  = $this->webPath . str_replace('f_', 's_', $file->pathname);
        $file->largeURL  = $this->webPath . str_replace('f_', 'l_', $file->pathname);
        
        if(!file_exists(str_replace($this->webPath, $this->savePath, $file->middleURL))) $file->middleURL = $file->fullURL;
        if(!file_exists(str_replace($this->webPath, $this->savePath, $file->smallURL)))  $file->smallURL  = $file->fullURL;
        if(!file_exists(str_replace($this->webPath, $this->savePath, $file->largeURL)))  $file->largeURL  = $file->fullURL;
        
        $file->isImage = true;
    }
    
    return $file;
}

在上传的附件图片预览中发现使用3.0的迁移方法缺少图片宽度和图片高度的处理,对saveUpload()重新修改如下

public function saveUpload($objectType = '', $objectID = '', $extra = '')
{
    $fileTitles = array();
    $now        = helper::now();
    $files      = $this->getUpload();
    
    $this->storage = $this->app->loadClass('saestorage');
    foreach($files as $id => $file)
    {   
        $imageSize = array('width' => 0, 'height' => 0);
        
        $this->storage->upload($this->config->sae->storage->domain, $file['pathname'], $file['tmpname']);
        if(in_array(strtolower($file['extension']), $this->config->file->imageExtensions))
        {
            $this->compressImage($file['pathname']);
            $imageSize = $this->getImageSize($file['tmpname']);
        }
        
        $file['objectType'] = $objectType;
        $file['objectID']   = $objectID;
        $file['addedBy']    = $this->app->user->account;
        $file['addedDate']  = $now;
        $file['extra']      = $extra;
        $file['width']      = $imageSize['width'];
        $file['height']     = $imageSize['height'];
        unset($file['tmpname']);
        $this->dao->insert(TABLE_FILE)->data($file)->exec();
        $fileTitles[$this->dao->lastInsertId()] = $file['title'];
    }
    return $fileTitles;
}
修改后生成缩略图功能暂时失效。

还需要对replaceFile进行修改

对File每个方法检查一遍,并进行相应的修改



演示http://chanz.sinaapp.com/  admin 123



  • 0
    点赞
  • 0
    收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页
评论

打赏作者

chujilu1991

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值