BrightSide Workbench Full Report + Source Code
ProfileImage.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2020 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.contacts.profile;
20 
21 import java.io.File;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
26 import org.turro.contacts.Contact;
27 import org.turro.contacts.db.ContactsPU;
28 import org.turro.elephant.context.ElephantContext;
29 import org.turro.file.util.FileAttach;
30 import org.zkoss.zul.Image;
31 
36 public class ProfileImage extends Image {
37 
38  private Contact contact;
39 
40  public void setContact(Contact contact) {
41  this.contact = contact;
42  }
43 
44  @Override
45  public void setSrc(String src) {
46  super.setSrc(src);
47  saveImage();
48  }
49 
50  private void saveImage() {
51  if(getContent() != null) {
52  FileAttach fileAttach = new FileAttach(ContactsPU.getObjectPath(contact));
53  File newFile = new File(ElephantContext.getRealPath(fileAttach.getPublishable() + "/profile/face.png"));
54  if(!newFile.getParentFile().exists()) {
55  newFile.getParentFile().mkdirs();
56  }
57  try (FileOutputStream fos = new FileOutputStream(newFile)) {
58  fos.write(getContent().getByteData());
59  } catch (IOException ex) {
60  Logger.getLogger(ProfileImage.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
61  }
62  }
63  }
64 
65 }
static String getObjectPath(Object object)
Definition: ContactsPU.java:68