111
沙世明
2022-11-22 928399eceec50e3d37ea08669a12789a9410a9d2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
global with sharing class BatchFileUploadController {
    public String newUrl {get; set;}
    public String staticResource {get; set;}
 
    public BatchFileUploadController(){
        AWS_Integration_Info__mdt awsConfiguration = [SELECT App_Id__c,Token_URL__c,App_Secret__c,Host_URL__c FROM AWS_Integration_Info__mdt  WHERE DeveloperName = 'AWS_Default_Configuration'];
        if (awsConfiguration == null) {
            System.debug('AWS_Integration_Info__mdt没配置');
        }else {
            newUrl = awsConfiguration.Host_URL__c + '/api/file/batchupload';
            staticResource = JSON.serialize(PIHelper.getPIIntegrationInfo('Document'));
        }
    }
 
    // @RemoteAction
    // global static String attachBlob(String parentId, String attachmentId, String fileName, String contentType, String base64BlobValue){
    //     /*
    //     parentId: The sfdc object Id this file will be attached to
    //     attachmentId: The record of the current Attachment file being processed
    //     fileName: Name of the attachment
    //     contentTye: Content Type of the file being attached
    //     base64BlobValue: Base64 encoded string of the file piece currently processing
    //     */
               
    //     //If recordId is blank this is the first part of a multi piece upload
    //     if(attachmentId == '' || attachmentId == null){
    //         Attachment att = new Attachment(
    //             ParentId = parentId,
    //             Body = EncodingUtil.Base64Decode(base64BlobValue),
    //             Name = fileName,
    //             ContentType = contentType
    //         );
    //         insert att;
            
    //         //Return the new attachment Id
    //         return att.Id;
            
    //     }else{
    //         for(Attachment atm : [select Id, Body from Attachment where Id = :attachmentId]){
    //             //Take the body of the current attachment, convert to base64 string, append base64 value sent from page, then convert back to binary for the body
    //             update new Attachment(Id = attachmentId, Body = EncodingUtil.Base64Decode(EncodingUtil.Base64Encode(atm.Body) + base64BlobValue));
    //         }
            
    //         //Return the Id of the attachment we are currently processing
    //         return attachmentId;
    //     }
    // }
 
    global class Response{
        public String recordId{set;get;}
        public String message{set;get;}
        public String status{set;get;}
    }
 
    @RemoteAction
    global static Response saveFile(String fileName,String key,String transId,String parentId){
        FileAddress__c file = new FileAddress__c();
        PIHelper.PIIntegration pI=PIHelper.getPIIntegrationInfo('Document');
        // 去除filename里得“&” zhj 2022-11-17
        fileName = fileName.remove('&');
        file.DownloadLink__c =pI.undeleteUrl+key+'&fileName='+fileName;
        file.FileName__c =fileName;
        file.ViewLink__c =pI.queryUrl+key;
        file.ParentRecordId__c =parentId;
        file.AWS_File_Key__c = key;
        Response response =new Response();
        Savepoint sp = Database.setSavepoint();
        try {
            insert file;
            //插入日志 
            //update 2022-11-17 加入新的日志方式
            PIHelper.saveTransLog('Document',key,transId,file.Id,JSON.serialize(file),'success','');
            response.recordId=file.Id;
            response.status='success';
            return response;
        } catch (Exception e) {
            System.debug('into catch'+e.getMessage());
            PIHelper.saveTransLog('Document',key,transId,file.Id,JSON.serialize(file),'fail',e.getMessage());
            Database.rollback(sp);
            response.message=e.getMessage();
            response.status='fail';
            return response;
        }
    }
}